我已经在permission_handler软件包上工作了一段时间。尝试找出当用户永久拒绝应用程序访问权限时,iOS或Android的应用程序设置页面。
我的应用程序首先需要访问媒体库和相机。有经验的Flutter开发人员请尽可能详细地解释。我是Flutter开发的新手,也是整个移动应用程序开发的新手。
这是iOS上的podfile:
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
target.build_configurations.each do |config|
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
## dart: PermissionGroup.calendar
'PERMISSION_EVENTS=0',
## dart: PermissionGroup.reminders
'PERMISSION_REMINDERS=0',
## dart: PermissionGroup.contacts
# 'PERMISSION_CONTACTS=0',
## dart: PermissionGroup.camera
# 'PERMISSION_CAMERA=1',
## dart: PermissionGroup.microphone
# 'PERMISSION_MICROPHONE=0',
## dart: PermissionGroup.speech
'PERMISSION_SPEECH_RECOGNIZER=0',
## dart: PermissionGroup.photos
# 'PERMISSION_PHOTOS=1',
## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
'PERMISSION_LOCATION=0',
## dart: PermissionGroup.notification
# 'PERMISSION_NOTIFICATIONS=0',
## dart: PermissionGroup.mediaLibrary
# 'PERMISSION_MEDIA_LIBRARY=1',
## dart: PermissionGroup.sensors
'PERMISSION_SENSORS=0'
]
end
end
end
这里是info.plist文件-
<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires access to your photo gallery to view or save images</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string></string>
这是我的实际代码,在按钮按下时,我正在检查是否有权限打开画廊。
TextButton.icon(
icon: const Icon(
Icons.photo_library_rounded,
size: 50,
),
label: const Text('Pick from gallery',
style: TextStyle(fontSize: 20)),
onPressed: () {
PermissionService().openPhotoGallery();
},
),
这是我的权限类。该应用程序不会打开应用程序设置,而是仅在iOS上加载设置。
import 'package:permission_handler/permission_handler.dart';
class PermissionService {
openPhotoGallery() async {
var galleryAccessStatus = await Permission.mediaLibrary.status;
if(!galleryAccessStatus.isGranted) {
var status = await Permission.mediaLibrary.request();
if(status.isPermanentlyDenied) {
await openAppSettings();
}
}
}
}
在查看Zeeyans的答案后,添加以下代码片段。openAppSettings不会打开应用程序设置页面,而是会打开“设置”页面。
![enter image description here](https://istack.dev59.com/EcofQ.webp)