如果要对PHAsset
进行更改,请将其打包在performChanges
块中。您在完成块中获得了成功Bool
和错误NSError
。现在,我想在请求失败的情况下向用户显示警报。这就是诀窍:
PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
let request = PHAssetChangeRequest(forAsset: asset)
request.creationDate = date
}, completionHandler: { (success: Bool, error: NSError?) -> Void in
dispatch_async(dispatch_get_main_queue()) {
if let error = error {
//present alert
}
}
})
问题是,当用户点击“不允许”时,它也会显示警报。我不想这样做,用户故意取消它,所以没有必要通知他们失败了。但是我怎样才能发现发生了什么?错误userInfo
为nil
,它似乎无法提供检测该案例的任何有用信息。我错过了什么吗?
答案 0 :(得分:0)
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status){
NSLog(@"%ld",(long)status);
switch (status) {
case PHAuthorizationStatusAuthorized:
// code for display photos
NSLog(@"ffefwfwfwef");
case PHAuthorizationStatusRestricted:
break;
case PHAuthorizationStatusDenied:
//code for Dont Allow code
break;
default:
break;
}
}];
答案 1 :(得分:0)
现在可以通过检查 error
是否为 PHPhotosError
,如果是,则检查其 code
以查看它是否为 .userCancelled
。
PHPhotoLibrary.shared().performChanges({
let request = PHAssetChangeRequest(forAsset: asset)
request.creationDate = date
}) { success, error in
guard let error = error else { return }
guard (error as? PHPhotosError)?.code != .userCancelled else { return }
DispatchQueue.main.async {
//present alert
}
}