2

当您想对 a 进行更改时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
        }
    }
})

问题是当用户点击不允许时,它也会显示警报。我不想这样做,用户故意取消了它,所以没有必要通知他们它失败了​​。但是我怎样才能检测到发生了什么?错误userInfonil,它似乎没有提供任何有用的信息来检测这种情况。我错过了什么吗?

4

2 回答 2

0

[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus状态){

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

}];

于 2016-04-19T10:23:17.387 回答
0

这现在可以通过检查是否error是 aPHPhotosError并且如果是检查它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
    }
}
于 2021-08-07T16:45:36.437 回答