我有一个单页应用程序,用户可以点按该图像,ImagePickerController
显示用于从相机胶卷中选择图像。
当基于Tab View控制器的更大项目中包含相同的代码时,我收到错误“此应用程序无法访问您的照片和视频”。
我也尝试在info.plist
中加入“隐私 - 照片库使用说明”,但没有运气。有没有人遇到过这个问题?
任何帮助将不胜感激。
答案 0 :(得分: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];
}
感谢您的指导。我使用了上面的逻辑并创建了以下swift代码:
@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)
}
现在我看到状态以Not Determined开头,然后是Denied。在隐私设置中,我没有看到列出我的应用程序的名称。