[iOS 开发] app无法访问本地相册,且不显示在设置 -隐私 - 照片中

时间:2024-09-10 23:04:56

近几天在使用iOS8的Photos Framework访问本地相册时,app即不会弹出是否允许访问提示框,也无法显示在iPhone的设置-隐私-照片的访问列表中,代码如下:

   PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus];
if (authorizationStatus == PHAuthorizationStatusAuthorized) {
[self getCameraRollAlbum]; //获取本地照片
} else {
[self initNoPermissionView]; //显示无权限的提示view
}

调试发现,[PHPhotoLibrary authorizationStatus] 返回的是 PHAuthorizationStatusNotDetermined。

之后在苹果的官方文档中的authorizationStatus 方法的最后看到这么一段话:

If this method returns PHAuthorizationStatusNotDetermined, you can call the requestAuthorization:method to prompt the user for photo library access permission.

意思是如果authorizationStatus返回的是PHAuthorizationStatusNotDetermined,可以通过调用 requestAuthorization: 方法来提示用户是否允许该app访问用户相册。

之后就在代码中添加了这个方法,并重新运行app:

  PHAuthorizationStatus authorizationStatus = [PHPhotoLibrary authorizationStatus];
if (authorizationStatus == PHAuthorizationStatusAuthorized) {
[self getCameraRollAlbum]; //获取本地照片
} else {
if (authorizationStatus == PHAuthorizationStatusNotDetermined) {
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { //弹出访问权限提示框
if (status == PHAuthorizationStatusAuthorized) {
[self getCameraRollAlbum];
} else {
[self initNoPermissionView];
}
}];
} else {
[self initNoPermissionView]; //显示无权限的提示view
} }

之后尝试访问本地相册的时候会主动弹出提示框,无论选择是或否,app都会显示在设置-隐私-照片 列表中。

[iOS 开发] app无法访问本地相册,且不显示在设置 -隐私 - 照片中    [iOS 开发] app无法访问本地相册,且不显示在设置 -隐私 - 照片中