无法处理NSPhotoLibraryAddUsageDescription

时间:2018-03-02 13:51:15

标签: swift ios11 uiactivityviewcontroller phphotolibrary

我正在使用uiactivityviewcontroller;其中有一个选项可以将照片保存到设备。

所以我添加了NSPhotoLibraryAddUsageDescription密钥以获取权限,

但无法处理权限,一旦用户点击Don't allow再没点击同一选项保存照片就没有任何反应;

我看到了这个链接:Determine if the access to photo library is set or not - PHPhotoLibrary

但它没有帮助;在这两种情况下(允许或不允许),它都会返回PHAuthorizationStatusNotDetermined

我已经尝试过PHPhotoLibrary.requestAuthorization了。它适用于读取和写入权限。我只想添加照片;所以只需要写权限。

有办法处理此权限吗?

1 个答案:

答案 0 :(得分:0)

我也遇到了同样的问题,这是我的解决方案,我的处理方式。

 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];

             }
             }
         }];

    }


}

}

//检查对图片库的访问权限

-(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];

    }

}
相关问题