当权限被永久拒绝时,Flutter permission_handler 包无法打开应用程序设置
Flutter permission_handler package not opening app settings when permission is permanently denied
已经在 permission_handler 包上工作了一段时间。当用户永久拒绝应用程序访问时,试图在 ios 或 android 上找出应用程序设置页面。
我的应用程序首先需要访问媒体库和相机。有经验的 flutter 开发者请尽可能详细解释。我是 flutter 开发的新手,也是整个移动应用程序开发的新手。
这是 ios
上的播客文件
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();
},
),
这是我的权限 class。该应用不会打开应用设置,而只是加载 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();
}
}
}
}
在查看 Zeeyan 的回答后添加此片段。 openAppSettings 不会打开应用程序设置页面,而是打开设置页面。
替换
!galleryAccessStatus.isGranted 与 galleryAccessStatus != PermissionStatus.granted
和
status.isPermanentlyDenied 状态为 != PermissionStatus.granted
openPhotoGallery() async {
var galleryAccessStatus = await Permission.mediaLibrary.status;
if(galleryAccessStatus != PermissionStatus.granted) { //here
var status = await Permission.mediaLibrary.request();
if(status != PermissionStatus.granted) { //here
await openAppSettings();
}
}
}
已经在 permission_handler 包上工作了一段时间。当用户永久拒绝应用程序访问时,试图在 ios 或 android 上找出应用程序设置页面。
我的应用程序首先需要访问媒体库和相机。有经验的 flutter 开发者请尽可能详细解释。我是 flutter 开发的新手,也是整个移动应用程序开发的新手。
这是 ios
上的播客文件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();
},
),
这是我的权限 class。该应用不会打开应用设置,而只是加载 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();
}
}
}
}
在查看 Zeeyan 的回答后添加此片段。 openAppSettings 不会打开应用程序设置页面,而是打开设置页面。
替换 !galleryAccessStatus.isGranted 与 galleryAccessStatus != PermissionStatus.granted 和 status.isPermanentlyDenied 状态为 != PermissionStatus.granted
openPhotoGallery() async {
var galleryAccessStatus = await Permission.mediaLibrary.status;
if(galleryAccessStatus != PermissionStatus.granted) { //here
var status = await Permission.mediaLibrary.request();
if(status != PermissionStatus.granted) { //here
await openAppSettings();
}
}
}