React Native踩坑记录
- Linking模块
- Android
- AndroidManifest.xml
- iOS
- Info.plist
- 端口被占用
- Android
- iOS
Linking模块
Linking 可以打开其他应用或者被其他应用打开。
canOpenURL(url:string)确定已安装的应用是否可以处理给定的 URL。
openURL(url:string)尝试使用任何已安装的应用打开给定的 URL。
Android
Linking.canOpenURL一直返回false是因为从Android 30开始,Google引入了对应用程序访问其他程序的限制。为了解决这个问题,需要在AndroidManifest.xml中添加一些配置。
AndroidManifest.xml
- <manifest>标签下:
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
上述配置可根据应用包名查找是否安装该应用。
- <application>标签下:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="https"/>
</intent-filter>
上述配置允许应用查询所有安装的应用程序以及处理http和https链接。
iOS
iOS9后引入了App白名单的策略,如果使用 canOpenURL方法,该方法所涉及到的 URL Schemes 必须在 Info.plist 中列为白名单。
Info.plist
以高德、百度地图为例:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>baidumap</string>
<string>iosamap</string>
</array>
端口被占用
Android
运行时更改端口号为8081(默认)以外的任意端口即可,以下都以8088端口为例:
yarn android --port=8088
iOS
运行时更改端口号后,
yarn ios --port=8088
报如下错误:
Make sure you’re running a packager server or have included a .jsbundle file in your application bundle.
因为新端口号和项目默认8081端口号不一致,导致监听不了。
xcode打开工程(双击 project name/ios/.xcworkspace,注意 0.60 版本之后的主项目文件是.xcworkspace,不是.xcodeproj)。在xcode工程里全局搜索 8081 ,替换成8088 即可。