forked from lemberg/ios-permissions-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SystemPermissionService.swift
71 lines (63 loc) · 2.03 KB
/
SystemPermissionService.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// SystemPermissionController.swift
//
// Created by Volodymyr Hyrka on 2/8/16.
// Copyright © 2016 LembergSolutions. All rights reserved.
//
import Foundation
import UIKit
enum PermissonStatus: Int {
case NotDetermined
case Restricted
case Denied
case Authorized
}
protocol PermissonConfiguration {
init()
func restrictedAlertMessage() -> String
func deniedAlertMessage() -> String
func checkStatus() -> PermissonStatus
func requestStatus(requestGranted: (successRequestResult: Bool) -> Void)
}
extension PermissonConfiguration {
func deniedAlertMessage() -> String {
return "You can enable access in Privacy Settings"
}
}
class Permission<T:PermissonConfiguration>{
private var checker:T
init() {
checker = T()
}
func preparePermission(sender: UIViewController, granted: (granted: Bool) -> Void) {
let status = checker.checkStatus()
let alertController = UIAlertController(title: "Access Denied", message: nil, preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Default,handler: nil))
switch status {
case .NotDetermined:
checker.requestStatus({ (successRequestResult) -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
granted(granted: successRequestResult)
})
})
return
case .Authorized:
granted(granted: true)
return
case .Restricted:
alertController.message = checker.restrictedAlertMessage()
granted(granted: false)
case .Denied:
alertController.message = checker.deniedAlertMessage()
let settingsAction = UIAlertAction(title: "Settings", style: .Default) { (_) -> Void in
let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
if let url = settingsUrl {
UIApplication.sharedApplication().openURL(url)
}
}
alertController.addAction(settingsAction)
granted(granted: false)
}
sender.presentViewController(alertController, animated: true, completion: nil)
}
}