Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added the possibility to use a custom view controller for displaying permission #80

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Permission.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -73,6 +75,8 @@
6DF9C2BD1C8F4FE5000710C1 /* Photos.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Photos.swift; sourceTree = "<group>"; };
6DF9C2BF1C8F5003000710C1 /* Events.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Events.swift; sourceTree = "<group>"; };
6DF9C2C51C8F5B4C000710C1 /* Location.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Location.swift; sourceTree = "<group>"; };
C23C024E1DDF2EA100E8448A /* UIAlertController+Extension.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "UIAlertController+Extension.swift"; sourceTree = "<group>"; };
C2ED3A731DDE2401001071FF /* Permissionable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Permissionable.swift; sourceTree = "<group>"; };
D08FF2891DC3AD2900F28088 /* PermissionFlags.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; path = PermissionFlags.xcconfig; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -124,6 +128,8 @@
6D0069B31C1868E8002FDB42 /* PermissionSet.swift */,
6D491E771C9CA90B00611006 /* PermissionStatus.swift */,
6D491E751C9CA7DE00611006 /* PermissionType.swift */,
C23C024E1DDF2EA100E8448A /* UIAlertController+Extension.swift */,
C2ED3A731DDE2401001071FF /* Permissionable.swift */,
);
path = Source;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand All @@ -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 */,
Expand Down
3 changes: 3 additions & 0 deletions Source/Permission.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 17 additions & 14 deletions Source/PermissionAlert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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
}
Expand All @@ -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) {
Expand All @@ -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
}
Expand All @@ -147,7 +150,7 @@ internal class PrePermissionAlert: PermissionAlert {
confirm = "Confirm"
}

fileprivate func confirmHandler(_ action: UIAlertAction) {
fileprivate func confirmHandler() {
permission.requestAuthorization(callbacks)
}
}
15 changes: 15 additions & 0 deletions Source/Permissionable.swift
Original file line number Diff line number Diff line change
@@ -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)
}
25 changes: 25 additions & 0 deletions Source/UIAlertController+Extension.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}