3

I am using uiactivityviewcontroller; In which there is a option to save photo to device.

So I added NSPhotoLibraryAddUsageDescription key for permission,

But not able to handle permission, once user click on Don't allow nothing happens when again click on same option to save photo;

I saw this link: Determine if the access to photo library is set or not - PHPhotoLibrary

But it didn't help; In both cases (Allow or Don't Allow) it is returning PHAuthorizationStatusNotDetermined

And I have already tried PHPhotoLibrary.requestAuthorization. It is for both read and write permissions. I only want to add photo; so need write permission only.

Is there a way to handle this permission?

JeanR
  • 76
  • 5
  • can you post your code requesting photo library access and the code saving your photo – adamfowlerphoto Mar 06 '18 at 17:59
  • As I am using UIActivityViewController it will be done automatically; I just have to add the key 'NSPhotoLibraryAddUsageDescription' in info.plist file.And the save option is given automatically by the controller @Spads – JeanR Mar 10 '18 at 03:41

1 Answers1

0

I have also faced the same problem, here is my solution, how i have handled.

 NSArray *dataToShare= @[@"Test",@"",[self contentURLofCurrentPreviewItem]];
                UIActivityViewController* activityViewController =
                [[UIActivityViewController alloc] initWithActivityItems:dataToShare
                                                  applicationActivities:nil];


        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
            [self presentViewController:activityViewController animated:YES completion:nil];
        }
        else {
            // Change Rect to position Popover
            UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:activityViewController];
            activityViewController.popoverPresentationController.sourceView = view;
            [popup presentPopoverFromRect:view.frame
                                                    inView:view.superview
                                  permittedArrowDirections:UIPopoverArrowDirectionAny
                                                  animated:YES];

        }

        if ([PHPhotoLibrary authorizationStatus] == PHAuthorizationStatusNotDetermined)
        {
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status)
             {
                 if (status == PHAuthorizationStatusDenied) {
                     [self showAlert];
                 }
             }];
        }


        [activityViewController setCompletionWithItemsHandler:^(NSString *activityType, BOOL completed, NSArray *returnedItems, NSError *activityError)
         {
             if ([activityType isEqualToString:@"com.apple.UIKit.activity.SaveToCameraRoll"])
             {

             if (![self getaccess])
             {
                 [self showAlert];

             }
             }
         }];

    }


}

}

// check access of Photo Library

-(bool)getaccess
{
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

if (status == PHAuthorizationStatusAuthorized)
{
    return YES;
    // Access has been granted.
}

else if (status == PHAuthorizationStatusDenied) {
     return NO;
    // Access has been denied.
}
else if (status == PHAuthorizationStatusRestricted)
{
    return NO;
    // Restricted access - normally won't happen.
}
return NO;

}

-(void)showAlert
{
    NSString *message = @"Save Image option required access of Photo Library. Please allow Photos app access for Your App in the Settings application.";


    UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:NSLocalizedString(@"Your App", nil)
                                  message:NSLocalizedString(message,nil)
                                  preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction *settingsAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Go To Settings", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
    {
         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
    }];
    [alert addAction:settingsAction];

    UIAlertAction *Cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action)
                                     {
                                         //do something when click button
                                     }];
    [alert addAction:Cancel];

    [self presentViewController:alert animated:YES completion:nil];

    }

}