Ask permission to access Camera Roll

后端 未结 7 1281
生来不讨喜
生来不讨喜 2020-11-28 22:39

I have a settings view where the user can choose switch on or off the feature \'Export to Camera Roll\'

When the user switches it on for the first time (and

7条回答
  •  天命终不由人
    2020-11-28 22:47

    Since iOS 8 with Photos framework use:

    Swift 3.0:

    PHPhotoLibrary.requestAuthorization { status in
        switch status {
        case .authorized:
            <#your code#>
        case .restricted:
            <#your code#>
        case .denied:
            <#your code#>
        default:
            // place for .notDetermined - in this callback status is already determined so should never get here
            break
        }
    }
    

    Objective-C:

    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        switch (status) {
            case PHAuthorizationStatusAuthorized:
                <#your code#>
                break;
            case PHAuthorizationStatusRestricted:
                <#your code#>
                break;
            case PHAuthorizationStatusDenied:
                <#your code#>
                break;
            default:
                break;
        }
    }];
    

    Important note from documentation:

    This method always returns immediately. If the user has previously granted or denied photo library access permission, it executes the handler block when called; otherwise, it displays an alert and executes the block only after the user has responded to the alert.

提交回复
热议问题