Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter permission Handler grant not showing on iOS

Tags:

flutter

dart

i created a class to ask for permission immediately it get to login, it show on Android but on iOs i am not seeing any permission grant.

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.");
    }
  }
}

and i call this function in the initstate of the Login.dart

like image 728
Gbenga B Ayannuga Avatar asked Jul 31 '21 06:07

Gbenga B Ayannuga


1 Answers

The permission_handler package introduced a breaking change in version 8.0.0, see changelog. Permissions on iOS are disabled by default, and you have the set the correct GCC_PREPROCESSOR_DEFINITIONS in you Podfile. An example Podfile can be found here, but basically you have to add this to you Podfile, set the permissions that you don't use to 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

Edit 1: Once that is done, save your Podfile and then stop the current instance of the project since hot restart will not reflect the changes. Rebuild the project and permission_handler requests should now be working perfectly.

Edit 2: As suggested by @YugankaSharan it might be necessary to run pod install for the changes to take effect.

like image 131
Peter Koltai Avatar answered Oct 02 '22 23:10

Peter Koltai