diff --git a/Permission.xcodeproj/project.pbxproj b/Permission.xcodeproj/project.pbxproj index df14805..3ae5920 100644 --- a/Permission.xcodeproj/project.pbxproj +++ b/Permission.xcodeproj/project.pbxproj @@ -32,6 +32,8 @@ 6DF9C2BE1C8F4FE5000710C1 /* Photos.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DF9C2BD1C8F4FE5000710C1 /* Photos.swift */; }; 6DF9C2C01C8F5003000710C1 /* Events.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DF9C2BF1C8F5003000710C1 /* Events.swift */; }; 6DF9C2C61C8F5B4C000710C1 /* Location.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6DF9C2C51C8F5B4C000710C1 /* Location.swift */; }; + C23C024F1DDF2EA100E8448A /* UIAlertController+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = C23C024E1DDF2EA100E8448A /* UIAlertController+Extension.swift */; }; + C2ED3A741DDE2401001071FF /* Permissionable.swift in Sources */ = {isa = PBXBuildFile; fileRef = C2ED3A731DDE2401001071FF /* Permissionable.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -73,6 +75,8 @@ 6DF9C2BD1C8F4FE5000710C1 /* Photos.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Photos.swift; sourceTree = ""; }; 6DF9C2BF1C8F5003000710C1 /* Events.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Events.swift; sourceTree = ""; }; 6DF9C2C51C8F5B4C000710C1 /* Location.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Location.swift; sourceTree = ""; }; + C23C024E1DDF2EA100E8448A /* UIAlertController+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIAlertController+Extension.swift"; sourceTree = ""; }; + C2ED3A731DDE2401001071FF /* Permissionable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Permissionable.swift; sourceTree = ""; }; D08FF2891DC3AD2900F28088 /* PermissionFlags.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = PermissionFlags.xcconfig; sourceTree = ""; }; /* End PBXFileReference section */ @@ -124,6 +128,8 @@ 6D0069B31C1868E8002FDB42 /* PermissionSet.swift */, 6D491E771C9CA90B00611006 /* PermissionStatus.swift */, 6D491E751C9CA7DE00611006 /* PermissionType.swift */, + C23C024E1DDF2EA100E8448A /* UIAlertController+Extension.swift */, + C2ED3A731DDE2401001071FF /* Permissionable.swift */, ); path = Source; sourceTree = ""; @@ -289,6 +295,7 @@ buildActionMask = 2147483647; files = ( 6D491E781C9CA90B00611006 /* PermissionStatus.swift in Sources */, + C23C024F1DDF2EA100E8448A /* UIAlertController+Extension.swift in Sources */, 6D935F5F1C9A14AB00BB39E3 /* Motion.swift in Sources */, 6D935F5D1C9A0FEA00BB39E3 /* Bluetooth.swift in Sources */, 6DF9C2B81C8F4F8F000710C1 /* Notifications.swift in Sources */, @@ -302,6 +309,7 @@ 6DF9C2C61C8F5B4C000710C1 /* Location.swift in Sources */, 6DA8B4B21BFB8AD8007A94FC /* PermissionAlert.swift in Sources */, 6C04FAF61CCA8F3A00B3F361 /* AddressBook.swift in Sources */, + C2ED3A741DDE2401001071FF /* Permissionable.swift in Sources */, 3D42A7DB1D5F66B300236ABA /* SpeechRecognizer.swift in Sources */, 6DF9C2BE1C8F4FE5000710C1 /* Photos.swift in Sources */, 6DF9C2BA1C8F4FAC000710C1 /* Microphone.swift in Sources */, diff --git a/Source/Permission.swift b/Source/Permission.swift index 162dd29..ae55590 100644 --- a/Source/Permission.swift +++ b/Source/Permission.swift @@ -126,6 +126,9 @@ open class Permission: NSObject { } #endif + /// The alert Class type, conforming to Protocol Permissionable + open var alertClass: Permissionable.Type = UIAlertController.self + /// The permission domain. open let type: PermissionType diff --git a/Source/PermissionAlert.swift b/Source/PermissionAlert.swift index 8d1d45d..edd108f 100644 --- a/Source/PermissionAlert.swift +++ b/Source/PermissionAlert.swift @@ -22,10 +22,15 @@ // SOFTWARE. // +import UIKit + open class PermissionAlert { /// The permission. fileprivate let permission: Permission + //the alert controller class + fileprivate let alertClass: Permissionable.Type + /// The status of the permission. fileprivate var status: PermissionStatus { return permission.status } @@ -61,26 +66,26 @@ open class PermissionAlert { fileprivate var cancelActionTitle: String? fileprivate var defaultActionTitle: String? - var controller: UIAlertController { - let controller = UIAlertController(title: title, message: message, preferredStyle: .alert) + var controller: Permissionable { + let controller = alertClass.alertController(title: title, message: message, type: self.type, status: self.status) - let action = UIAlertAction(title: cancelActionTitle, style: .default, handler: cancelHandler) - controller.addAction(action) + controller.addAction(title: cancelActionTitle, style: .default, handler: cancelHandler) return controller } internal init(permission: Permission) { self.permission = permission + self.alertClass = permission.alertClass } internal func present() { DispatchQueue.main.async { - UIApplication.shared.presentViewController(self.controller) + UIApplication.shared.presentViewController(self.controller as! UIViewController) } } - fileprivate func cancelHandler(_ action: UIAlertAction) { + fileprivate func cancelHandler() { callbacks(status) } } @@ -96,11 +101,10 @@ internal class DisabledAlert: PermissionAlert { } internal class DeniedAlert: PermissionAlert { - override var controller: UIAlertController { + override var controller: Permissionable { let controller = super.controller - let action = UIAlertAction(title: defaultActionTitle, style: .cancel, handler: settingsHandler) - controller.addAction(action) + controller.addAction(title: defaultActionTitle, style: .cancel, handler: settingHandler) return controller } @@ -119,7 +123,7 @@ internal class DeniedAlert: PermissionAlert { callbacks(status) } - private func settingsHandler(_ action: UIAlertAction) { + private func settingHandler() { NotificationCenter.default.addObserver(self, selector: .settingsHandler, name: .UIApplicationDidBecomeActive) if let URL = URL(string: UIApplicationOpenSettingsURLString) { @@ -129,11 +133,10 @@ internal class DeniedAlert: PermissionAlert { } internal class PrePermissionAlert: PermissionAlert { - override var controller: UIAlertController { + override var controller: Permissionable { let controller = super.controller - let action = UIAlertAction(title: defaultActionTitle, style: .cancel, handler: confirmHandler) - controller.addAction(action) + controller.addAction(title: defaultActionTitle, style: .cancel, handler: confirmHandler) return controller } @@ -147,7 +150,7 @@ internal class PrePermissionAlert: PermissionAlert { confirm = "Confirm" } - fileprivate func confirmHandler(_ action: UIAlertAction) { + fileprivate func confirmHandler() { permission.requestAuthorization(callbacks) } } diff --git a/Source/Permissionable.swift b/Source/Permissionable.swift new file mode 100644 index 0000000..d8086f5 --- /dev/null +++ b/Source/Permissionable.swift @@ -0,0 +1,15 @@ +// +// Permissionable.swift +// Permission +// +// Created by Gautier Gdx on 17/11/2016. +// Copyright © 2016 delba. All rights reserved. +// + +import UIKit + +public protocol Permissionable: class { + static func alertController(title: String?,message: String?,type: PermissionType,status: PermissionStatus) -> Permissionable + + func addAction(title: String?, style: UIAlertActionStyle, handler: @escaping () -> Void) +} diff --git a/Source/UIAlertController+Extension.swift b/Source/UIAlertController+Extension.swift new file mode 100644 index 0000000..99e4c45 --- /dev/null +++ b/Source/UIAlertController+Extension.swift @@ -0,0 +1,25 @@ +// +// UIAlertController+Extension.swift +// Permission +// +// Created by Gautier Gdx on 17/11/2016. +// Copyright © 2016 delba. All rights reserved. +// + +import UIKit + + +extension UIAlertController: Permissionable { + + public static func alertController(title: String?,message: String?,type: PermissionType,status: PermissionStatus) -> Permissionable { + let alertViewController = UIAlertController(title: title, message: message, preferredStyle: .alert) + return alertViewController + } + + public func addAction(title: String?, style: UIAlertActionStyle, handler: @escaping () -> Void) { + let action = UIAlertAction(title: title, style: style) { _ in + handler() + } + addAction(action) + } +}