This repository has been archived by the owner on Jul 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 389
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
518 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
ALCameraViewController/Utilities/PhotoLibraryAuthorizer.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// PhotoLibraryAuthorizer.swift | ||
// ALCameraViewController | ||
// | ||
// Created by Alex Littlejohn on 2016/03/26. | ||
// Copyright © 2016 zero. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import Photos | ||
|
||
public typealias PhotoLibraryAuthorizerCompletion = (error: NSError?) -> Void | ||
|
||
class PhotoLibraryAuthorizer { | ||
|
||
private let errorDomain = "com.zero.imageFetcher" | ||
private let completion: PhotoLibraryAuthorizerCompletion | ||
|
||
init(completion: PhotoLibraryAuthorizerCompletion) { | ||
self.completion = completion | ||
handleAuthorization(PHPhotoLibrary.authorizationStatus()) | ||
} | ||
|
||
func onDeniedOrRestricted() { | ||
let error = errorWithKey("error.access-denied", domain: errorDomain) | ||
completion(error: error) | ||
} | ||
|
||
func handleAuthorization(status: PHAuthorizationStatus) { | ||
switch status { | ||
case .NotDetermined: | ||
PHPhotoLibrary.requestAuthorization(handleAuthorization) | ||
break | ||
case .Authorized: | ||
dispatch_async(dispatch_get_main_queue()) { | ||
self.completion(error: nil) | ||
} | ||
break | ||
case .Denied, .Restricted: | ||
dispatch_async(dispatch_get_main_queue()) { | ||
self.onDeniedOrRestricted() | ||
} | ||
break | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// UIButtonExtensions.swift | ||
// ALCameraViewController | ||
// | ||
// Created by Alex Littlejohn on 2016/03/26. | ||
// Copyright © 2016 zero. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
typealias ButtonAction = () -> Void | ||
|
||
extension UIButton { | ||
|
||
private struct AssociatedKeys { | ||
static var ActionKey = "ActionKey" | ||
} | ||
|
||
private class ActionWrapper { | ||
let action: ButtonAction | ||
init(action: ButtonAction) { | ||
self.action = action | ||
} | ||
} | ||
|
||
var action: ButtonAction? { | ||
set(newValue) { | ||
removeTarget(self, action: #selector(UIButton.performAction), forControlEvents: UIControlEvents.TouchUpInside) | ||
var wrapper: ActionWrapper? | ||
if let newValue = newValue { | ||
wrapper = ActionWrapper(action: newValue) | ||
addTarget(self, action: #selector(UIButton.performAction), forControlEvents: UIControlEvents.TouchUpInside) | ||
} | ||
|
||
objc_setAssociatedObject(self, &AssociatedKeys.ActionKey, wrapper, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) | ||
} | ||
get { | ||
guard let wrapper = objc_getAssociatedObject(self, &AssociatedKeys.ActionKey) as? ActionWrapper else { | ||
return nil | ||
} | ||
|
||
return wrapper.action | ||
} | ||
} | ||
|
||
func performAction() { | ||
if let a = action { | ||
a() | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.