Flutter插件permission_handler获取权限不
2022-06-08 本文已影响0人
Hamiltonian
如果往已有的iOS工程中,集成Flutter Module。Flutter Module中又集成了permission_handler,来获取各种权限。那么你会遇到各种权限都是denied,即使你已经开启授权。即使你按照官网配置iOS工程,还是如此。
原因是官方配置只适用纯Flutter App,如果是iOS嵌入Flutter模块不适合官网配置
- 官方配置【适用于纯Flutter App,iOS嵌入Flutter不适用!!!】点击跳转官网
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
... # Here are some configurations automatically generated by flutter
# You can enable the permissions needed here. For example to enable camera
# permission, just remove the `#` character in front so it looks like this:
#
# ## dart: PermissionGroup.camera
# 'PERMISSION_CAMERA=1'
#
# Preprocessor definitions can be found in: https://github.com/Baseflow/flutter-permission-handler/blob/master/permission_handler/ios/Classes/PermissionHandlerEnums.h
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
## dart: PermissionGroup.calendar
# 'PERMISSION_EVENTS=1',
## dart: PermissionGroup.reminders
# 'PERMISSION_REMINDERS=1',
## dart: PermissionGroup.contacts
# 'PERMISSION_CONTACTS=1',
## dart: PermissionGroup.camera
# 'PERMISSION_CAMERA=1',
## dart: PermissionGroup.microphone
# 'PERMISSION_MICROPHONE=1',
## dart: PermissionGroup.speech
# 'PERMISSION_SPEECH_RECOGNIZER=1',
## dart: PermissionGroup.photos
# 'PERMISSION_PHOTOS=1',
## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
# 'PERMISSION_LOCATION=1',
## dart: PermissionGroup.notification
# 'PERMISSION_NOTIFICATIONS=1',
## dart: PermissionGroup.mediaLibrary
# 'PERMISSION_MEDIA_LIBRARY=1',
## dart: PermissionGroup.sensors
# 'PERMISSION_SENSORS=1',
## dart: PermissionGroup.bluetooth
# 'PERMISSION_BLUETOOTH=1',
## dart: PermissionGroup.appTrackingTransparency
# 'PERMISSION_APP_TRACKING_TRANSPARENCY=1',
## dart: PermissionGroup.criticalAlerts
# 'PERMISSION_CRITICAL_ALERTS=1'
]
end
end
end
- 非官方配置【适用于Flutter Module的工程配置】,添加以下代码到iOS主项目的Podfile文件中!
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['ENABLE_BITCODE'] = 'YES'
# config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '11.0'
# config.build_settings["EXCLUDED_ARCHS"] = "armv7"
end
if target.name == "permission_handler"
target.build_configurations.each do |config|
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
## dart: PermissionGroup.calendar
'PERMISSION_EVENTS=1',
## dart: PermissionGroup.reminders
# 'PERMISSION_REMINDERS=1',
## dart: PermissionGroup.contacts
# 'PERMISSION_CONTACTS=1',
## dart: PermissionGroup.camera
# 'PERMISSION_CAMERA=1',
## dart: PermissionGroup.microphone
# 'PERMISSION_MICROPHONE=1',
## dart: PermissionGroup.speech
# 'PERMISSION_SPEECH_RECOGNIZER=1',
## dart: PermissionGroup.photos
# 'PERMISSION_PHOTOS=1',
## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
# 'PERMISSION_LOCATION=1',
## dart: PermissionGroup.notification
# 'PERMISSION_NOTIFICATIONS=1',
## dart: PermissionGroup.mediaLibrary
# 'PERMISSION_MEDIA_LIBRARY=1',
## dart: PermissionGroup.sensors
# 'PERMISSION_SENSORS=1',
## dart: PermissionGroup.bluetooth
# 'PERMISSION_BLUETOOTH=1',
## dart: PermissionGroup.appTrackingTransparency
# 'PERMISSION_APP_TRACKING_TRANSPARENCY=1',
## dart: PermissionGroup.criticalAlerts
# 'PERMISSION_CRITICAL_ALERTS=1'
]
end
end
end
end
问题解决了,休息下。回来思考:为什么要这样配置?
3b9c58166c9be1b9a2e0c164eb53f851.jpeg原理:我们都知道,Flutter调用原生的API需要通过插件来实现,permission_handler就是获取原生手机权限。
permission_handler.jpeg代码跑起来要经过四步 : 源代码-> 预处理->汇编->链接,然而在预处理缺少的宏定义,不会报错,但是会影响逻辑。
这就是为什么跑起来没问题,权限状态有问题!
解决记录:
- 进入permission_handler插件的iOS源码,里面大量逻辑依赖预处理的宏定义。
- 对应Podfile中的GCC_PREPROCESSOR_DEFINITIONS和Xcode Build Settings中的Apple Clang - Preprocessing。要添加对应的宏定义,才能正确运行。
- 添加到Podfile中,每次执行pod install会自动配置到 Xcode Build Settings中。
- Podfile中奇怪的语法其实是Ruby语言写的。
// 如果没有定义PERMISSION_EVENTS,以下代码就不会执行!
#if PERMISSION_EVENTS
return [EventPermissionStrategy permissionStatus:EKEntityTypeEvent];
#endif