Detect Permission of Media Library iOS

Detect permission of media library ios

-(void) checkMediaLibraryPermissions {
[MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus status){
switch (status) {
case MPMediaLibraryAuthorizationStatusNotDetermined: {
// not determined
break;
}
case MPMediaLibraryAuthorizationStatusRestricted: {
// restricted
break;
}
case MPMediaLibraryAuthorizationStatusDenied: {
// denied
break;
}
case MPMediaLibraryAuthorizationStatusAuthorized: {
// authorized
break;
}
default: {
break;
}
}
}];
}

How to know what library requests particular iOS permissions in Flutter?

Run this command:

flutter pub deps | grep permission_handler

If you get any output at all, then the package is being pulled in. IF that is the package that's requesting permissions, there is a way to customize exclude/disable the imported permission libraries, in your Podfile. Find this section:

post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
... (you may have additional customizations here)

end
end

And add some flags to disable those permissions, by adding a flag such as 'PERMISSION_CAMERA=0', to the permissions you want to disable, like so:

post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)

target.build_configurations.each do |config| # <-- start adding from here

# You can remove unused permissions here
# for more infomation: https://github.com/BaseflowIT/flutter-permission-handler/blob/develop/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=0',

## dart: PermissionGroup.reminders
'PERMISSION_REMINDERS=0',

## dart: PermissionGroup.contacts
# 'PERMISSION_CONTACTS=0',

## dart: PermissionGroup.camera
# 'PERMISSION_CAMERA=0',

## dart: PermissionGroup.microphone
'PERMISSION_MICROPHONE=0',

## dart: PermissionGroup.speech
'PERMISSION_SPEECH_RECOGNIZER=0',

## dart: PermissionGroup.photos
# 'PERMISSION_PHOTOS=0',

## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse]
'PERMISSION_LOCATION=0',

## dart: PermissionGroup.notification
# 'PERMISSION_NOTIFICATIONS=0',

## dart: PermissionGroup.mediaLibrary
'PERMISSION_MEDIA_LIBRARY=0',

## dart: PermissionGroup.sensors
'PERMISSION_SENSORS=0',

## dart: PermissionGroup.bluetooth
'PERMISSION_BLUETOOTH=0'
]
end # <-- end adding here
end
end

In the example above, I've commented out (because my apps uses them) contacts, camera, photos, and notifications; and disables all the other permissions.

Hopefully permission_handler is the package requesting the permission, because they make it easy to fix.

Determine if the access to photo library is set or not - PHPhotoLibrary

Check +[PHPhotoLibrary authorizationStatus] – if not set, it will return PHAuthorizationStatusNotDetermined. (You can then request access using +requestAuthorization: on the same class.)

How to check in Swift if the user gave permission to access the photolibrary with UIImagePicker?

You can use the below method to check the permission status for the gallery.
and if the permission is denied then also it navigates you to the app setting screen.

func checkGalleryPermission()
{
let authStatus = PHPhotoLibrary.authorizationStatus()
switch authStatus
{
case .denied : print("denied status")
let alert = UIAlertController(title: "Error", message: "Photo library status is denied", preferredStyle: .alert)
let cancelaction = UIAlertAction(title: "Cancel", style: .default)
let settingaction = UIAlertAction(title: "Setting", style: UIAlertAction.Style.default) { UIAlertAction in
if let url = URL(string: UIApplication.openSettingsURLString) {
UIApplication.shared.open(url, options: [:], completionHandler: { _ in })
}
}
alert.addAction(cancelaction)
alert.addAction(settingaction)
Viewcontoller.present(alert, animated: true, completion: nil)
break
case .authorized : print("success")
//open gallery
break
case .restricted : print("user dont allowed")
break
case .notDetermined : PHPhotoLibrary.requestAuthorization({ (newStatus) in
if (newStatus == PHAuthorizationStatus.authorized) {
print("permission granted")
//open gallery
}
else {
print("permission not granted")
}
})
break
case .limited:
print("limited")
@unknown default:
break
}
}

iOS - Getting Callback On Media Library Access Alert By System

You need to ask for the permission for accessing MPMediaLibrary, this is how you can request for it

Objective-C

[MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus authorizationStatus) {
if ( authorizationStatus == MPMediaLibraryAuthorizationStatusAuthorized ) {
// Reload your list here
} else {
// user did not authorize
} }];

Swift

MPMediaLibrary.requestAuthorization { (status) in
if status == .authorized {
// Reload your list here
} else {
// user did not authorize
}
}

Not show Media library permission dialog in Xamarin iOS

During research, I found the solution here.

Security Privacy Enhancements

This happened because I used deprecated description key.

I resolved this issue by using NSAppleMusicUsageDescription instead.

Request authorization to Media Library programmatically fails

The MPMediaLibrary will only automatically prompt the user once. The state is MPMediaLibraryAuthorizationStatusNotDetermined if you ask for it before it has been granted or denied by the user. If they have denied access previously, you need to send the user to the System Settings so they can manually turn it on for your app.

The following code is how we are doing it.

+ (void) validateMediaLibraryForMinimumIosAndAboveWithViewController:(UIViewController *)viewController
ifAuthorized:(void(^)())authorizedBlock
ifNotAuthorized:(void(^)())notAuthorizedBlock
{
MPMediaLibraryAuthorizationStatus authorizationStatus = MPMediaLibrary.authorizationStatus;

switch (authorizationStatus)
{
case MPMediaLibraryAuthorizationStatusAuthorized:
{
// We are already authorized - proceed
if( authorizedBlock )
{
authorizedBlock();
}
break;
}
case MPMediaLibraryAuthorizationStatusNotDetermined:
{
// Not yet authorized - request it from the system
[MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus authorizationStatus)
{
if ( authorizationStatus == MPMediaLibraryAuthorizationStatusAuthorized )
{
if( authorizedBlock )
{
authorizedBlock();
}
}
else
{
PLog(@"The Media Library was not authorized by the user");
if( notAuthorizedBlock )
{
notAuthorizedBlock();
}
}
}];
break;
}

case MPMediaLibraryAuthorizationStatusRestricted:
case MPMediaLibraryAuthorizationStatusDenied:
{
// user has previously denied access. Ask again with our own alert that is similar to the system alert
// then take them to the System Settings so they can turn it on for the app
NSString *titleString = NSLocalizedStringWithDefaultValue(@"Media Library Privacy Alert Title",
@"Localizable",
[NSBundle mainBundle],
@"Would Like to Access Apple Music And Your Media Library",
@"Title for dialog requesting media library access");

[self displayPermissionAlertFromViewController:viewController
withTitle:titleString];
if( notAuthorizedBlock )
{
notAuthorizedBlock();
}
break;
}
}
}

+ (void)displayPermissionAlertFromViewController:(UIViewController *)viewController withTitle:(NSString *)title
{
NSString* appName = [[NSProcessInfo processInfo] processName];

NSString *titleString = [NSString stringWithFormat:@"\"%@\" %@",appName, title];

NSString *cancelString = NSLocalizedStringWithDefaultValue(@"Don't Allow",
@"Localizable",
[NSBundle mainBundle],
@"Don't Allow",
@"Don't allow button");

NSString *settingsString = NSLocalizedStringWithDefaultValue( @"Settings",
@"Localizable",
[NSBundle mainBundle],
@"Settings",
@"Settings button label");

UIAlertController *alertController = [UIAlertController
alertControllerWithTitle:titleString
message:nil
preferredStyle:UIAlertControllerStyleAlert];

[alertController addAction:[UIAlertAction actionWithTitle:cancelString
style:UIAlertActionStyleDefault
handler:nil]];

[alertController addAction:[UIAlertAction actionWithTitle:settingsString
style:UIAlertActionStyleDefault
handler:
^(UIAlertAction * _Nonnull action)
{
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
[[UIApplication sharedApplication] openURL:url];
}]];

[viewController presentViewController:alertController animated:true completion:nil];
}


Related Topics



Leave a reply



Submit