iOS相机权限获取

19 阅读2分钟
语言:Flutter
问题:获取相机弹窗的权限不出来,iOS系统设置里面对应app也没有相机选项。是宏没有打开
解决方法:打开宏

Podfile

# post_install do |installer|
#   installer.pods_project.targets.each do |target|
#     flutter_additional_ios_build_settings(target)
#     # 为所有 Pod 目标启用 dSYM 生成
#     target.build_configurations.each do |config|
#       if config.name == 'Release'
#         config.build_settings['DEBUG_INFORMATION_FORMAT'] = 'dwarf-with-dsym'
#       end
#     end
#   end
# end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)

    target.build_configurations.each do |config|

      if config.name == 'Release'
        config.build_settings['DEBUG_INFORMATION_FORMAT'] = 'dwarf-with-dsym'
      end

      #  permission_handler 必须的宏配置
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
        '$(inherited)',

        'PERMISSION_CAMERA=1',

      ]

    end
  end
end

这段 Podfile 的 post_install 配置是正确的


一、post_install 是什么 🔧

post_install do |installer|

意思是:

CocoaPods 安装完成后执行这段脚本。

也就是在执行:

pod install

之后运行。

用途:

  • 修改 iOS 编译参数
  • 给插件加配置
  • 修复 Flutter / iOS 插件问题

Flutter 权限插件基本都用这个。


二、installer.pods_project.targets.each do |target|

installer.pods_project.targets.each do |target|

意思是:

遍历所有 Pod 插件。

例如:

  • permission_handler
  • mobile_scanner
  • ffmpeg_kit
  • camera
  • etc

这段代码会对 所有插件生效

相当于:

给所有插件统一设置编译参数。


三、flutter_additional_ios_build_settings(target)

flutter_additional_ios_build_settings(target)

意思:

Flutter 自动生成的 iOS配置。

它会自动设置:

  • Swift版本
  • 架构支持
  • Bitcode
  • iOS版本兼容性

这是 Flutter 官方必须的一行。

不能删。


四、target.build_configurations.each do |config|

target.build_configurations.each do |config|

意思:

遍历 Debug / Release 编译模式。

iOS有三种模式:

  • Debug(开发)
  • Profile(性能分析)
  • Release(发布)

这段代码:

对每个模式分别设置参数。


五、DEBUG_INFORMATION_FORMAT = dwarf-with-dsym

if config.name == 'Release'
  config.build_settings['DEBUG_INFORMATION_FORMAT'] = 'dwarf-with-dsym'
end

意思:

Release版本生成 dSYM 文件。

作用:

  • 崩溃日志解析
  • Crashlytics
  • TestFlight 崩溃分析

否则:

  • 崩溃日志看不懂

这是一个好习惯配置 👍

和权限无关。


六、最重要的一段(权限宏)⭐

config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [  '$(inherited)',  'PERMISSION_CAMERA=1',]

这是关键。

意思:

给 iOS 编译器设置宏定义。

等价于:

#define PERMISSION_CAMERA 1

告诉 permission_handler:

编译相机权限代码。


七、$(inherited) 是什么

'$(inherited)',

意思:

保留系统原来的设置。

如果不写:

可能破坏 Flutter 默认配置。

必须保留。

相当于:

在原来的基础上增加权限宏。


八、PERMISSION_CAMERA=1 是什么 ⭐

'PERMISSION_CAMERA=1',

意思:

打开相机权限模块。

permission_handler 内部代码类似:

#if PERMISSION_CAMERA
// 相机权限代码
#endif

如果没有:

#if PERMISSION_CAMERA

条件不成立。

相机代码不会编译进去。

结果就是:

  • request() 不弹窗
  • denied
  • 设置里没有权限

之前就是这个问题。


九、整体逻辑总结(核心理解)

配置现在等价于:

安装所有插件
Flutter配置生效
Release版本生成崩溃符号
开启相机权限模块

流程:

pod install
   ↓
执行 post_install
   ↓
给所有插件加 PERMISSION_CAMERA=1
   ↓
Xcode 编译
   ↓
permission_handler 包含相机权限代码
   ↓
request() 正常弹窗

十、建议升级为完整权限配置(推荐)🔥

建议改成:

config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [  '$(inherited)',  'PERMISSION_CAMERA=1',  'PERMISSION_PHOTOS=1',  'PERMISSION_MICROPHONE=1',]

原因:

以后大概率会用:

  • 相册
  • 麦克风
  • 视频

否则未来又会踩坑。


十一、最关键结论

现在这段:

'PERMISSION_CAMERA=1',

就是让:

Permission.camera.request();

真正生效的原因。

没有它:

iOS 会表现为:

  • 不弹窗
  • denied
  • 设置没有权限