dart flutter权限处理程序授予未在iOS上显示

omtl5h9j  于 2023-09-28  发布在  Flutter
关注(0)|答案(3)|浏览(137)

我创建了一个类来请求权限,它可以立即登录,它在Android上显示,但在iOS上我看不到任何权限授予。

class PermissionService {
  Future permissionHandler() async {
    await Permission.contacts.shouldShowRequestRationale;
    if (await Permission.contacts.request().isGranted) {
      // Either the permission was already granted before or the user just granted it.
    }
    Map<Permission, PermissionStatus> statuses = await [
      Permission.locationWhenInUse,
      Permission.locationAlways,
      Permission.photos,
      Permission.camera,
      Permission.location,
      Permission.microphone,
      Permission.notification,
    ].request();

    if (statuses[Permission.location].isDenied) {
      print("Location permission is denied.");
    }

    if (statuses[Permission.camera].isDenied) {
      print("Camera permission is denied.");
    }
    if (statuses[Permission.photos].isDenied) {
      print("Photos permission is denied.");
    }
    if (statuses[Permission.notification].isDenied) {
      print("Notification permission is denied.");
    }
    if (statuses[Permission.microphone].isDenied) {
      print("Microphone permission is denied.");
    }
    if (statuses[Permission.locationWhenInUse].isDenied) {
      print("locationWhenInUse permission is denied.");
    }
    if (statuses[Permission.locationAlways].isDenied) {
      print("locationAlways permission is denied.");
    }
  }
}

我在Login.dart的initstate中调用这个函数

eoigrqb6

eoigrqb61#

permission_handler包在8.0.0版本中引入了一个突破性的更改,请参见changelog。默认情况下,iOS上的权限是禁用的,您可以在Podfile中设置正确的GCC_PREPROCESSOR_DEFINITIONS。可以找到一个示例Podfile here,但基本上你必须将其添加到你的Podfile中,将你不使用的权限设置为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
      # for more infomation: https://github.com/BaseflowIT/flutter-permission-handler/blob/master/permission_handler/ios/Classes/PermissionHandlerEnums.h
      # e.g. when you don't need camera permission, just add 'PERMISSION_CAMERA=0'
      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

编辑1:完成后,保存您的Podfile,然后停止项目的当前示例,因为热重启不会反映更改。重新构建项目,permission_handler请求现在应该可以正常工作了。
编辑2:正如@YugankaSharan所建议的,可能需要运行pod install才能使更改生效。

5w9g7ksd

5w9g7ksd2#

在我的情况下,我也使用这个图书馆。但问题不在于图书馆。该应用程序甚至没有显示一个对话框,要求位置权限。然后我去设置,去应用程序,我没有看到请求的位置权限界面。

问题出在Info.plist文件上。

<key>NSLocationAlwaysUsageDescription</key> 
<string>Cho phép Chat truy cập thông tin vị trí để chia sẻ vị trí trong cuộc trò chuyện</string> 
<key>NSLocationWhenInUseUsageDescription</key> 
<string>Cho phép Chat truy cập thông tin vị trí để chia sẻ vị trí trong cuộc trò chuyện</string>

权限NSLocationAlwaysUsageDescription高于NSLocationWhenInUseUsageDescription错误。因此,应该颠倒顺序。

mwngjboj

mwngjboj3#

具体来说,在某些软件包(例如Alarm软件包)中,您可能需要从“签名和功能”部分打开某些功能。牢记

相关问题