You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native新架构iOS端:RCTPresentedViewController无法打开UIImagePickerController

React Native新架构iOS调用UIImagePickerController相机无响应排查方案

核心排查点

  • 权限配置缺失:iOS相机/相册权限未在Info.plist中声明时,系统会静默拦截相机调用,无日志报错。需添加以下键值对:
    <key>NSCameraUsageDescription</key>
    <string>需要访问相机拍摄照片</string>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>需要访问相册选择照片</string>
    
  • 控制器present逻辑错误:
    • 避免嵌套使用self.presentedViewController?.present,直接获取当前活跃的顶层控制器进行present:
      if let topVC = UIApplication.shared.connectedScenes
          .filter({$0.activationState == .foregroundActive})
          .compactMap({$0 as? UIWindowScene})
          .first?.windows
          .filter({$0.isKeyWindow}).first?.rootViewController {
              topVC.present(vc, animated: true)
      }
      
    • 调用前验证self.viewController实例有效性:
      guard let currentVC = self.viewController else {
          print("当前控制器实例为空")
          return
      }
      currentVC.present(vc, animated: true)
      
  • UIImagePickerController配置问题:
    • 必须设置sourceType为.camera,同时提前检查设备相机可用性:
      guard UIImagePickerController.isSourceTypeAvailable(.camera) else {
          print("设备不支持相机")
          return
      }
      let picker = UIImagePickerController()
      picker.sourceType = .camera
      picker.delegate = self
      
  • 线程问题:UI操作必须在主线程执行,确保present逻辑包裹在主线程调度中:
    DispatchQueue.main.async {
        // 执行present相机控制器的代码
    }
    

额外验证步骤

  • 检查Xcode控制台的隐私权限警告(可能被其他日志覆盖)
  • 在原生ViewController中直接调用相机,排除React Native桥接层的影响
  • 确认UIImagePickerControllerDelegate已正确实现并绑定代理

内容的提问来源于stack exchange,提问作者BraveEvidence

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 20:42:29