宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//
//  LD_AuthorizesTool.swift
//  JQTools
//
//  Created by 无故事王国 on 2020/8/18.
//
 
import Foundation
import Photos
import AssetsLibrary
import MediaPlayer
import CoreTelephony
import CoreLocation
import AVFoundation
 
 
/// 权限 此类废弃,单独使用,上架时会扫描到未使用但要求添加plist权限
public class LD_AuthorizesTool:NSObject{
    public static let `default` = LD_AuthorizesTool()
    let manager = CLLocationManager()
    
    public enum LD_PermissionsType{
        /// 相机
        case camera
        /// 相册
        case photo
        /// 位置
        case location
        /// 网络
        case network
        /// 麦克风
        case microphone
        /// 媒体库
        case media
    }
    
    override init() {
        super.init()
    }
    
    /// 相册权限
    @discardableResult
    public func photoLibraryAuth()->Bool{
        let status = PHPhotoLibrary.authorizationStatus()
        
        switch status {
            case .authorized:
                return true
            
            case .notDetermined:
                // 请求授权
                PHPhotoLibrary.requestAuthorization({ (status) -> Void in
                    DispatchQueue.main.async(execute: { () -> Void in
                        self.photoLibraryAuth()
                    })
                })
            
            default: ()
            DispatchQueue.main.async(execute: { () -> Void in
                let alertController = UIAlertController(title: "照片访问受限",message: "点击“设置”,允许访问您的照片",preferredStyle: .alert)
                let cancelAction = UIAlertAction(title:"取消", style: .cancel, handler:nil)
                let settingsAction = UIAlertAction(title:"设置", style: .default, handler: {
                    (action) -> Void in
                    let url = URL(string: UIApplication.openSettingsURLString)
                    if let url = url, UIApplication.shared.canOpenURL(url) {
                        if #available(iOS 10, *) {
                            UIApplication.shared.open(url, options: [:],completionHandler: {(success) in
                                
                            })
                        } else {
                            UIApplication.shared.openURL(url)
                        }
                    }
                })
                
                alertController.addAction(cancelAction)
                alertController.addAction(settingsAction)
                LD_currentViewController().present(alertController, animated: true, completion: nil)
            })
        }
        return false
    }
    
    /// 麦克风,相机权限
    @discardableResult
//    public func captureDeviceAuth(type:AVMediaType)->Bool{
//
//        let status = AVCaptureDevice.authorizationStatus(for: type)
//
//        switch status {
//            case .authorized:
//                return true
//
//            case .notDetermined:
//                // 请求授权
//                AVCaptureDevice.requestAccess(for: AVMediaType.audio, completionHandler: {
//                    (status) in
//                    DispatchQueue.main.async(execute: { () -> Void in
//                        self.captureDeviceAuth(type: type)
//                    })
//                })
//            default: ()
//            DispatchQueue.main.async(execute: { () -> Void in
//
//                var title = ""
//                var message = ""
//
//                if type == .audio{
//                    title = "麦克风访问受限"
//                    message = "点击“设置”,允许访问您的麦克风"
//                }else if type == .video{
//                    title = "相机访问受限"
//                    message = "点击“设置”,允许访问您的相机"
//                }
//
//                let alertController = UIAlertController(title: title,message: message,preferredStyle: .alert)
//                let cancelAction = UIAlertAction(title:"取消", style: .cancel, handler:nil)
//                let settingsAction = UIAlertAction(title:"设置", style: .default, handler: {
//                    (action) -> Void in
//                    let url = URL(string: UIApplication.openSettingsURLString)
//                    if let url = url, UIApplication.shared.canOpenURL(url) {
//                        if #available(iOS 10, *) {
//                            UIApplication.shared.open(url, options: [:],completionHandler: {(success) in
//
//                            })
//                        } else {
//                            UIApplication.shared.openURL(url)
//                        }
//                    }
//                })
//
//                alertController.addAction(cancelAction)
//                alertController.addAction(settingsAction)
//                LD_currentViewController().present(alertController, animated: true, completion: nil)
//            })
//        }
//        return false
//    }
    
//    @available(iOS 9.3, *)
//    public func openMediaPlayerServiceWithBlock(_ isSet:Bool? = nil,_ action :@escaping ((Bool)->())) {
//        let authStatus = MPMediaLibrary.authorizationStatus()
//        if authStatus == MPMediaLibraryAuthorizationStatus.notDetermined {
//            MPMediaLibrary.requestAuthorization { (status) in
//                if (status == MPMediaLibraryAuthorizationStatus.authorized) {
//                    DispatchQueue.main.async {
//                        action(true)
//                    }
//                }else{
//                    DispatchQueue.main.async {
//                        action(false)
//                        if isSet == true {self.openURL(.media)}
//                    }
//                }
//            }
//        } else if authStatus == MPMediaLibraryAuthorizationStatus.authorized {
//            action(true)
//        } else {
//            action(false)
//             if isSet == true {openURL(.media)}
//        }
//    }
 
    // MARK: - 检测是否开启联网
    /// 检测是否开启联网
    public func openEventServiceWithBolck(_ isSet:Bool? = nil,_ action :@escaping ((Bool)->())) {
        let cellularData = CTCellularData()
        cellularData.cellularDataRestrictionDidUpdateNotifier = { (state) in
            if state == CTCellularDataRestrictedState.restrictedStateUnknown ||  state == CTCellularDataRestrictedState.notRestricted {
                action(false)
                if isSet == true {self.openURL(.network)}
            } else {
                action(true)
            }
        }
        let state = cellularData.restrictedState
        if state == CTCellularDataRestrictedState.restrictedStateUnknown ||  state == CTCellularDataRestrictedState.notRestricted {
            action(false)
            if isSet == true {openURL(.network)}
        } else {
            action(true)
        }
    }
 
    // MARK: - 检测是否开启定位
    /// 检测是否开启定位
    public func openLocationServiceWithBlock(_ isSet:Bool? = nil,_ action :@escaping ((Bool)->())) {
        var isOpen = false
    //    if CLLocationManager.locationServicesEnabled() || CLLocationManager.authorizationStatus() != .denied {
    //        isOpen = true
    //    }
        if CLLocationManager.authorizationStatus() != .restricted && CLLocationManager.authorizationStatus() != .denied {
            isOpen = true
        }
        if isOpen == false && isSet == true {openURL(.location)}
        action(isOpen)
    }
 
    // MARK: - 检测是否开启摄像头
    /// 检测是否开启摄像头 (可用)
    public func openCaptureDeviceServiceWithBlock(_ isSet:Bool? = nil,_ action :@escaping ((Bool)->())) {
        let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
        if authStatus == AVAuthorizationStatus.notDetermined {
            AVCaptureDevice.requestAccess(for: AVMediaType.video) { (granted) in
                action(granted)
                if granted == false && isSet == true {self.openURL(.camera)}
            }
        } else if authStatus == AVAuthorizationStatus.restricted || authStatus == AVAuthorizationStatus.denied {
            action(false)
            if isSet == true {openURL(.camera)}
        } else {
            action(true)
        }
    }
    
    // MARK: - 检测是否开启相册
    /// 检测是否开启相册
    public func openAlbumServiceWithBlock(_ isSet:Bool? = nil,_ action :@escaping ((Bool)->())) {
        var isOpen = true
        let authStatus = PHPhotoLibrary.authorizationStatus()
        if authStatus == PHAuthorizationStatus.restricted || authStatus == PHAuthorizationStatus.denied {
            isOpen = false;
            if isSet == true {openURL(.photo)}
        }
        action(isOpen)
    }
 
    // MARK: - 检测是否开启麦克风
    /// 检测是否开启麦克风
//    public func openRecordServiceWithBlock(_ isSet:Bool? = nil,_ action :@escaping ((Bool)->())) {
//        let permissionStatus = AVAudioSession.sharedInstance().recordPermission
//        if permissionStatus == AVAudioSession.RecordPermission.undetermined {
//            AVAudioSession.sharedInstance().requestRecordPermission { (granted) in
//                action(granted)
//                if granted == false && isSet == true {self.openURL(.microphone)}
//            }
//        } else if permissionStatus == AVAudioSession.RecordPermission.denied || permissionStatus == AVAudioSession.RecordPermission.undetermined{
//            action(false)
//            if isSet == true {openURL(.microphone)}
//        } else {
//            action(true)
//        }
//    }
    
    // MARK: - 跳转系统设置界面
    public func openURL(_ type: LD_PermissionsType? = nil) {
        let title = "访问受限"
        var message = "请点击“前往”,允许访问权限"
        let appName: String = (Bundle.main.infoDictionary!["CFBundleDisplayName"] ?? "") as! String //App 名称
        if type == .camera { // 相机
            message = "请在iPhone的\"设置-隐私-相机\"选项中,允许\"\(appName)\"访问你的相机"
        } else if type == .photo { // 相册
            message = "请在iPhone的\"设置-隐私-照片\"选项中,允许\"\(appName)\"访问您的相册"
        } else if type == .location { // 位置
            message = "请在iPhone的\"设置-隐私-定位服务\"选项中,允许\"\(appName)\"访问您的位置"
        } else if type == .network { // 网络
            message = "请在iPhone的\"设置-蜂窝移动网络\"选项中,允许\"\(appName)\"访问您的移动网络"
        } else if type == .microphone { // 麦克风
            message = "请在iPhone的\"设置-隐私-麦克风\"选项中,允许\"\(appName)\"访问您的麦克风"
        } else if type == .media { // 媒体库
            message = "请在iPhone的\"设置-隐私-媒体与Apple Music\"选项中,允许\"\(appName)\"访问您的媒体库"
        }
        let url = URL(string: UIApplication.openSettingsURLString)
        let alertController = UIAlertController(title: title,
                                                message: message,
                                                preferredStyle: .alert)
        let cancelAction = UIAlertAction(title:"取消", style: .cancel, handler:nil)
        let settingsAction = UIAlertAction(title:"前往", style: .default, handler: {
            (action) -> Void in
            if  UIApplication.shared.canOpenURL(url!) {
                if #available(iOS 10, *) {
                    UIApplication.shared.open(url!, options: [:],completionHandler: {(success) in})
                } else {
                    UIApplication.shared.openURL(url!)
                }
            }
        })
        alertController.addAction(cancelAction)
        alertController.addAction(settingsAction)
        UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
    }
}