使用 React Native + Expo 时,iOS API 在哪里访问?

Posted

技术标签:

【中文标题】使用 React Native + Expo 时,iOS API 在哪里访问?【英文标题】:When using React Native + Expo, where is the iOS API accessed? 【发布时间】:2021-12-27 16:00:40 【问题描述】:

我正在关注 Expo 教程,您可以在其中构建一个简单的图像共享应用程序 (https://docs.expo.dev/tutorial/image-picker/)。

我很好奇 node_modules 中访问 ios 权限接口的实际代码在哪里。

例如,在 app.js 我有以下代码:

let permissionResult = await ImagePicker.requestMediaLibraryPermissionsAsync();

requestMediaLibraryPermissionsAsync 方法位于我找到的 node_modules/expo-image-picker/build/ImagePicker.js 中:

export async function requestMediaLibraryPermissionsAsync(writeOnly = false) 
    const imagePickerMethod = ExponentImagePicker.requestMediaLibraryPermissionsAsync;
    return imagePickerMethod(writeOnly);

转到 node_modules/expo-image-picker/build/ExponentImagePicker.js 我发现:

import  NativeModulesProxy  from 'expo-modules-core';
export default NativeModulesProxy.ExponentImagePicker;
//# sourceMappingURL=ExponentImagePicker.js.map

这是我感到困惑的地方,我不确定如何继续跟踪模块。我在 node_modules/expo-modules-core/ios/interfaces/Permissions 中看到有与 iOS 权限相关的 Objective-c 文件 - 这些代码示例中是否使用了这些文件?我们实际上在代码中的什么地方访问了 iOS API?我有一种强烈的感觉,我从错误的角度来处理这个问题,所以请原谅我的无知。

【问题讨论】:

您访问原生 iOS API 的意图是什么?您想通过编写自定义本机代码来扩展 Expo 图像选择器功能吗? 我现在的主要目标是更深入地了解 React Native 和 Expo,因为我是移动开发的新手。通过查看 expo-image-picker 中的这种特定方法的工作原理,我希望了解 app.js 中的代码如何转换为本机实现的“幕后”,如果这有意义的话。 一般来说,每个 React 原生包需要访问 OS 原生模块都有 javascript 函数,这些函数绑定到每个设备平台的相应原生函数。最重要的是,Expo 有自己的抽象层,因为它的包在 Expo Go 中运行,没有直接的本地链接。您可以从了解如何使用 React Native 开发原生模块开始。 React 团队提供介绍 -reactnative.dev/docs/native-modules-intro 【参考方案1】:

我觉得你得看看仓库https://github.com/expo/expo/search?q=ExponentImagePicker

尝试找到您需要的 API 名称。

【讨论】:

以上是关于使用 React Native + Expo 时,iOS API 在哪里访问?的主要内容,如果未能解决你的问题,请参考以下文章

在 expo/react-native 中发送图像时随机出现“错误处理请求正文”

使用 react-native (expo) 切换手电筒

使用signInWithCredential(),React Native Expo时,Firestore权限被拒绝

React Native “Expo SDK 需要 Expo 才能运行......此代码未在 Expo 上运行。”

在 react-native-web (expo) 中聚焦时更改 TextInput 的边框颜色

使用 lottie-react-native (2.6.1) 时,React native expo 应用程序在 android 上崩溃,但适用于 ios