我正在使用 flutter 创建适用于 iOS 和 Android 的应用程序。我需要 iOS 和 Android 的
photos
和 storage
权限才能访问照片以更新用户个人资料图片。
我使用 permission_handler
包来请求权限。这在 Android 中运行良好,但在 iOS 中则不然。
<key>NSPhotoLibraryUsageDescription</key>
<string>We need Photos access to allow you to update Profile Picture.</string>
var permission = Platform.isAndroid
? Permission.storage
: Permission.photos;
var permissionStatus = await permission.request();
print("isGranted: " +
permissionStatus.isGranted.toString() +
" isDenied: " +
permissionStatus.isDenied.toString() +
" isLimited: " +
permissionStatus.isLimited.toString() +
" isRestricted: " +
permissionStatus.isRestricted.toString() +
" isPermanentlyDenied: " +
permissionStatus.isPermanentlyDenied.toString());
flutter:isGranted:false isDenied:false isLimited:false isRestricted:false isPermanentlyDenied:true
我尝试在模拟器(iPhone 12pro max)上卸载并重新安装该应用程序,但仍然无法请求权限(显示请求弹出窗口)。
首先将以下代码复制并粘贴到您的 podfile 中。然后设置
'PERMISSION_PHOTOS=1'
,如果你不想使用其他权限可以删除或设置它=0(exp 'PERMISSION_CAMERA=0'
,)。
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
# You can remove unused permissions here
# e.g. when you don't need camera permission, just add 'PERMISSION_CAMERA=0'
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
## dart: PermissionGroup.calendar
## dart: PermissionGroup.reminders
## dart: PermissionGroup.contacts
## 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]
## dart: PermissionGroup.notification
## dart: PermissionGroup.mediaLibrary
'PERMISSION_MEDIA_LIBRARY=1',
## dart: PermissionGroup.sensors
## dart: PermissionGroup.bluetooth
## dart: PermissionGroup.appTrackingTransparency
'PERMISSION_APP_TRACKING_TRANSPARENCY=1',
## dart: PermissionGroup.criticalAlerts
'PERMISSION_SENSORS=1'
]
end
end
end
然后将其添加到 info.plist 文件中
<key>NSPhotoLibraryUsageDescription</key>
<string>We need Photos access to allow you to update Profile Picture.</string>
最后,在您的项目中使用 permission_handler 插件,并在您想要请求权限的位置添加此代码。
var permissionStatus = await permission.request();
使用最新的
permission_handler:
包。
将照片权限添加到您的 Info.plist
文件。
<!-- Permission options for the `photos` group -->
<key>NSPhotoLibraryUsageDescription</key>
<string>photos</string>
将以下内容添加到您的 Podfile 文件中:
post_install do |installer|
installer.pods_project.targets.each do |target|
... # Here are some configurations automatically generated by flutter
# Start of the permission_handler configuration
target.build_configurations.each do |config|
# 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_apple/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 of the permission_handler configuration
end
end
如果您需要添加更多权限。删除您想要使用的权限前面的 # 字符。例如
## dart: PermissionGroup.photos
'PERMISSION_PHOTOS=1',
颤抖中:
var request;
var status = await Permission.photos.request();
if (status.permanentlyDenied) {
//Open app settings to manually enabling the permisisons
await openAppSettings();
} else {
//request for permissions
result = await Permission.photos.request().isGranted;
}
终于 清洁和重建
iOS 上权限包的行为已更改为默认拒绝所有权限。您必须更新 PList 并打开您希望在应用程序中使用的权限才能使其正常工作。
删除 Info.plist 底部的旧内容:'''post_install do...'''
并将其替换为:
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
# You can remove unused permissions here
# e.g. when you don't need camera permission, just add 'PERMISSION_CAMERA=0'
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
## dart: PermissionGroup.calendar
## dart: PermissionGroup.reminders
## dart: PermissionGroup.contacts
## 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]
## dart: PermissionGroup.notification
## dart: PermissionGroup.mediaLibrary
'PERMISSION_MEDIA_LIBRARY=1',
## dart: PermissionGroup.sensors
## dart: PermissionGroup.bluetooth
## dart: PermissionGroup.appTrackingTransparency
'PERMISSION_APP_TRACKING_TRANSPARENCY=1',
## dart: PermissionGroup.criticalAlerts
'PERMISSION_SENSORS=1'
]
end
end
end
根据https://stackoverflow.com/a/73825306/6610017[1]
为我工作