Expo与React分享组件iOS端适配报错及解决方案求助
问题解决指南:React Native 相册分享 iOS 端错误修复
一、API 废弃问题的正确修复
你遇到的第一个错误是因为使用了 iOS 10 起就废弃的 openURL: 方法,以下是符合当前 API 规范的跳转实现:
private func redirectToHostApp(type: RedirectType) { // 先校验 URL 合法性,避免空指针 guard let encodedKey = sharedKey.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), let url = URL(string: "\(shareProtocol)://dataUrl=\(encodedKey)#\(type.rawValue)") else { extensionContext?.completeRequest(returningItems: [], completionHandler: nil) return } var responder: UIResponder? = self while responder != nil { if let app = responder as? UIApplication { // 使用非废弃的 open 方法,在回调中结束扩展 app.open(url, options: [:]) { [weak self] success in if !success { print("跳转主App失败") } self?.extensionContext?.completeRequest(returningItems: [], completionHandler: nil) } return } responder = responder?.next } // 未找到 UIApplication 时直接结束扩展 extensionContext?.completeRequest(returningItems: [], completionHandler: nil) }
要点说明:
- 对
sharedKey做 URL 编码,避免特殊字符破坏 URL 格式 - 将扩展结束逻辑放在跳转回调中,确保跳转流程完成后再释放资源
- 移除不必要的 iOS 版本分支,
open(_:options:completionHandler:)从 iOS 10 开始支持,覆盖绝大多数版本
二、NSOSStatusErrorDomain Code=-10814 错误排查
这个错误的核心是系统无法识别你定义的 URL Scheme,按以下步骤逐一验证:
1. 主App URL Scheme 配置校验
打开主App的 Info.plist,确认 CFBundleURLTypes 配置正确:
<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleURLSchemes</key> <array> <string>ShareMedia</string> <!-- 必须与你的 shareProtocol 变量值完全一致 --> </array> <key>CFBundleURLName</key> <string>com.yourcompany.yourapp</string> <!-- 替换为你的App Bundle ID --> </dict> </array>
2. 添加 iOS 14+ 权限配置
在主App的 Info.plist 中添加 LSApplicationQueriesSchemes,允许系统查询你的自定义 Scheme:
<key>LSApplicationQueriesSchemes</key> <array> <string>ShareMedia</string> </array>
3. 验证 URL 格式合法性
确保生成的 URL 没有非法字符,比如:
sharedKey包含&、=、#等特殊字符时,必须做 URL 编码(已在第一步的代码中处理)type枚举的rawValue必须是合法的字符串,不能包含空格或特殊符号
4. 主App Linking 配置补全
在 AppDelegate.mm 中补充实现 Universal Links 处理方法(iOS 9+ 推荐):
- (BOOL)application:(UIApplication *)application continueUserActivity:(nonnull NSUserActivity *)userActivity restorationHandler:(nonnull void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler { return [RCTLinkingManager application:application continueUserActivity:userActivity restorationHandler:restorationHandler]; }
三、额外排查步骤
- 清理 Xcode Derived Data,重新构建主App和Share Extension
- 优先使用真机测试(模拟器对Share Extension的支持存在兼容性问题)
- 在 Safari 中直接输入你的 Scheme 链接(如
ShareMedia://dataUrl=test#media),验证是否能正常打开主App
内容的提问来源于stack exchange,提问作者art1go
相关产品推荐
相关产品推荐

