Advanced-NSOperations/Earthquakes/Operations/PhotosCondition.swift

67 lines
1.7 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
Copyright (C) 2015 Apple Inc. All Rights Reserved.
See LICENSE.txt for this samples licensing information
Abstract:
This file shows an example of implementing the OperationCondition protocol.
*/
#if os(iOS)
import Photos
/// A condition for verifying access to the user's Photos library.
struct PhotosCondition: OperationCondition {
static let name = "Photos"
static let isMutuallyExclusive = false
init() { }
func dependencyForOperation(operation: EarthquakeOperation) -> Operation? {
return PhotosPermissionOperation()
}
func evaluateForOperation(operation: EarthquakeOperation, completion: (OperationConditionResult) -> Void) {
switch PHPhotoLibrary.authorizationStatus() {
case .authorized:
completion(.Satisfied)
default:
let error = NSError(code: .ConditionFailed, userInfo: [
OperationConditionKey: type(of: self).name
])
completion(.Failed(error))
}
}
}
/**
A private `EarthquakeOperation` that will request access to the user's Photos, if it
has not already been granted.
*/
private class PhotosPermissionOperation: EarthquakeOperation {
override init() {
super.init()
addCondition(condition: AlertPresentation())
}
override func execute() {
switch PHPhotoLibrary.authorizationStatus() {
case .notDetermined:
DispatchQueue.main.async {
PHPhotoLibrary.requestAuthorization { status in
self.finish()
}
}
default:
finish()
}
}
}
#endif