Software authorization fails to request authorization

iOS 10 now requires user permission to access the Media Library. We check if we have access to the media library before we use it, and if we do not use [MPMediaLibrary requestAuthorization: to request authorization from the user.

I expect this to show the same popup request for access to the media library that we get when the application starts, but nothing happens. It just returns with the state MPMediaLibraryAuthorizationStatusDenied before that.

docs for requestAuthorization are currently incomplete, so I can’t tell if I am using it incorrectly, or something else is wrong.

  if ( MPMediaLibrary.authorizationStatus == MPMediaLibraryAuthorizationStatusAuthorized) { // we already have access to the Media Library - use it here... } else { // We expect this to show a popup so the user can grant access, but does not work [MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus authorizationStatus) { if ( authorizationStatus == MPMediaLibraryAuthorizationStatusAuthorized ) { // success: the user authorized - use it here... } else { // user did not authorize - tell user why here... } }]; } 

Update

There seems to be no way to return the original dialog box (see comments below). I now use this code to at least take me to the right place in the settings so that the user can make changes. (for iOS8 and above)

 NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString]; [[UIApplication sharedApplication] openURL:url]; 
+6
source share
1 answer

MPMediaLibrary will automatically query the user only once. The MPMediaLibraryAuthorizationStatusNotDetermined is if you request it before the user submits or rejects it. If they previously denied access, you need to send the user to System Preferences so that they can manually enable it for your application.

The following code is how we do 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]; } 
+5
source

All Articles