Flutter 在高版本Android和Ios没法通过permission-handler申请权限的解决方案

发布于 3 个月前 作者 phonegap100 327 次浏览 最后一次编辑是 3 个月前 来自 分享

下面我们给大家看看Flutter 在高版本Android和Ios没法通过permission-handler申请权限的解决方案,首先我们看看权限申请的Demo

1、例如:Flutter Android Ios 申请蓝牙权限Demo

1、找到插件https://pub.dev/packages/permission_handler

2、配置插件

dependencies:
  permission_handler: ^9.2.0

3、配置权限 Android In the android/app/src/main/AndroidManifest.xml let’s add:

 <uses-permission android:name="android.permission.BLUETOOTH" />  
	 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />  
	 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>  
 <application

IOS In the ios/Runner/Info.plist let’s add:

	<dict>  
	    <key>NSBluetoothAlwaysUsageDescription</key>  
	    <string>Need BLE permission</string>  
	    <key>NSBluetoothPeripheralUsageDescription</key>  
	    <string>Need BLE permission</string>  
	    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>  
	    <string>Need Location permission</string>  
	    <key>NSLocationAlwaysUsageDescription</key>  
	    <string>Need Location permission</string>  
	    <key>NSLocationWhenInUseUsageDescription</key>  
	    <string>Need Location permission</string>

4、申请权限


  Future<bool> requestPermission() async {
    bool hasBluetoothPermission = await requestBluePermission();
    if (hasBluetoothPermission) {
      print("蓝牙权限申请通过");
      setState(() {
        this.hasPermission = true;
      });
 
    } else {
      print("蓝牙权限申请不通过");
      this.hasPermission = false;
    }
    return hasBluetoothPermission;
  }
  Future<bool> requestBluePermission() async {
    //获取当前的权限
    var status = await Permission.bluetooth.status;
    if (status == PermissionStatus.granted) {
      //已经授权
      return true;
    } else {
      //未授权则发起一次申请
      status = await Permission.bluetooth.request();
      if (status == PermissionStatus.granted) {
        return true;
      } else {
        return false;
      }
    }
  }

上面方法在Android手机里面是没有问题的,但是Ios和Androidx中没法申请权限 这个时候我们需要配置permission_handler插件

2、Flutter Ios和Androidx中没法通过permission_handler申请权限,如何配置permission_handler插件

参考:https://pub.dev/packages/permission_handler

Androidx中的配置

1、将以下内容添加到您的“gradle.properties”文件中:

android.useAndroidX=true
android.enableJetifier=true

2、确保compileSdkVersion将“android/app/build.gradle”文件中的设置为 31:

android {
  compileSdkVersion 31
  ...
}

3、确保将所有android依赖项替换为它们的 AndroidX

Ios中的配置

1、增加下面代码到您的 Podfile file中:

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)',
        'PERMISSION_BLUETOOTH=1',
      ]
    end
  end
end

如下图

blue.jpg

如果还需要配置其他权限请参考https://pub.dev/packages/permission_handler

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

删除#您确实要使用的权限前面的字符。例如,如果您需要访问日历,请确保代码如下所示:

 ## dart: PermissionGroup.calendar
      'PERMISSION_EVENTS=1',
回到顶部