0

我有一个单页应用程序,用户可以在其中点击图像并ImagePickerController显示用于从相机胶卷中挑选图像。

当基于 Tab View 控制器的更大项目中包含相同的代码时,我收到错误“此应用程序无权访问您的照片和视频”。

我也尝试在其中包含“隐私 - 照片库使用说明”,info.plist但没有运气。有没有人遇到过这个问题?

任何帮助将不胜感激。

4

1 回答 1

0

您是否在 Apple 设置 > 隐私 > 照片中检查了您的隐私设置?iOS 应在首次使用时请求许可。如果用户拒绝,您所能做的就是告诉用户转到设置并允许访问。我猜这个设置对你来说是关闭的。

您可以在应用程序中检查授权状态。我在 Objective C 中编程,并且我使用的是仅 iOS 8+ 的类 PHPhotoLibrary,但也许下面的代码会给你一些想法。(还有一些“ShowMessage”伪代码。)

- (void) loadView {
    [self continueWithStatus: [PHPhotoLibrary authorizationStatus]];
}

- (void) continueWithStatus: (PHAuthorizationStatus) status {

    if (status == PHAuthorizationStatusRestricted)  ShowMessage "Access to photo library is restricted.";  
    else if (status == PHAuthorizationStatusDenied) ShowMessage "You need to enable access to photos.  Apple Settings > Privacy > Photos.";
    else if (status == PHAuthorizationStatusNotDetermined) {
        [PHPhotoLibrary requestAuthorization: ^(PHAuthorizationStatus status) {
            dispatch_async (dispatch_get_main_queue(), ^{   // continue work on main thread
                [self continueWithStatus: [PHPhotoLibrary authorizationStatus]];
            });
        }];
    }
    else [self startAssetRetrieve];
}

感谢您的指导。我使用了上述逻辑并创建了以下快速代码:

@IBAction func selectImageFromPhotoLibrary(sender:
 UITapGestureRecognizer) {
    authHandler(PHPhotoLibrary.authorizationStatus()) 
}
func authHandler(status: PHAuthorizationStatus) {
    switch status {
    case .Authorized:
        startAssetRevrival()
    case .Denied:
        print("denied")
    case .NotDetermined:
        print("not determined")
        PHPhotoLibrary.requestAuthorization(f)
    case .Restricted:
        print("restricted")
    }
  }
  func f(status: PHAuthorizationStatus){
    dispatch_async(dispatch_get_main_queue()) {
        self.authHandler(status)
    }
 }

 func startAssetRevrival() {
    let imagePickerController = UIImagePickerController()
    // Only allow photos to be picked, not taken.
    imagePickerController.sourceType = 
        UIImagePickerControllerSourceType.PhotoLibrary
    // Make sure ViewController is notified when the user picks an
       image.
    imagePickerController.delegate = self
    presentViewController(imagePickerController, animated: true,
    completion: nil)
}

现在我看到状态从“未确定”开始,然后到“拒绝”。在隐私设置中,我没有看到列出我的应用程序的名称。

于 2015-09-21T03:38:00.447 回答