-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermissionHelper.swift
70 lines (60 loc) · 2.11 KB
/
PermissionHelper.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
//
// PermissionHelper.swift
//
// Created by Harit Kothari on 16/12/16.
// Copyright © 2016 Simform. All rights reserved.
//
import UIKit
import Photos
import AVFoundation
class PermissionHelper: NSObject {
typealias PermissionResult = (_ result:Bool, _ message:String?, _ error:Error?) -> ()
override private init() { }
static let sharedInstance : PermissionHelper = {
let instance = PermissionHelper()
return instance
}()
func checkPhotoLibraryPermission(completion:@escaping PermissionResult) {
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized: //handle authorized status
completion(true, "", nil)
case .denied, .restricted: //handle denied status
completion(false, "", nil)
case .notDetermined: // ask for permissions
PHPhotoLibrary.requestAuthorization() { status in
switch status {
case .authorized:
completion(true, "", nil)
case .denied, .restricted:
completion(false, "", nil)
case .notDetermined:
// won't happen but still
completion(false, "not determined", nil)
}
}
}
}
func checkCameraPermission(completion:@escaping PermissionResult) {
if AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) == AVAuthorizationStatus.authorized
{
// Already Authorized
completion(true, "", nil)
}
else
{
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted :Bool) -> Void in
if granted == true
{
// User granted
completion(true, "", nil)
}
else
{
// User Rejected
completion(false, "", nil)
}
})
}
}
}