161 lines
5.8 KiB
Swift
161 lines
5.8 KiB
Swift
//
|
|
// Permission.swift
|
|
// JiuLaiBao
|
|
//
|
|
// Created by SeanXu on 2018/10/26.
|
|
// Copyright © 2018 DemoOrg. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import UserNotifications
|
|
import AVFoundation
|
|
import URLNavigator
|
|
import Photos
|
|
|
|
class Permission: NSObject {
|
|
|
|
public typealias Callback = (Bool) -> Void
|
|
public typealias SetAction = () -> Void
|
|
|
|
static func notificationAuthorizationStatus(_ callback: @escaping Callback) {
|
|
if #available(iOS 10.0, *) {
|
|
UNUserNotificationCenter.current().getNotificationSettings(completionHandler: { settings in
|
|
DispatchQueue.main.async {
|
|
switch settings.authorizationStatus {
|
|
case .authorized, .provisional:
|
|
callback(true)
|
|
case .denied:
|
|
callback(false)
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
})
|
|
} else {
|
|
if UIApplication.shared.currentUserNotificationSettings?.types.isEmpty == false {
|
|
callback(true)
|
|
} else {
|
|
callback(false)
|
|
}
|
|
}
|
|
}
|
|
|
|
// static func openAppSetting(title: String, message: String, cancelHandle: ((PopupAction) -> Void)? = nil ) {
|
|
// let popupVC = PopupViewController(title: title, detail: message,
|
|
// cancelTitle: "取消",
|
|
// cancelHandler: cancelHandle,
|
|
// confirmTitle: "前往设置") { _ in
|
|
// guard let settingsURL = URL(string: UIApplication.openSettingsURLString) else {
|
|
// return
|
|
// }
|
|
// if UIApplication.shared.canOpenURL(settingsURL) {
|
|
// if #available(iOS 10.0, *) {
|
|
// UIApplication.shared.open(settingsURL) { _ in }
|
|
// } else {
|
|
// UIApplication.shared.openURL(settingsURL)
|
|
// }
|
|
// }
|
|
// }
|
|
// UIViewController.topMost?.present(popupVC, animated: true, completion: nil)
|
|
// }
|
|
|
|
static func openAppSetting(title: String, message: String, setAction: SetAction?=nil) {
|
|
let alertVC = UIAlertController.init(title: title,
|
|
message: message,
|
|
preferredStyle: .alert)
|
|
let cancelAction = UIAlertAction.init(title: "取消", style: .cancel)
|
|
|
|
let setAction = UIAlertAction.init(title: "去设置", style: .default) { action in
|
|
if let setAction = setAction {
|
|
setAction()
|
|
return
|
|
}
|
|
guard let settingsURL = URL(string: UIApplication.openSettingsURLString) else {
|
|
return
|
|
}
|
|
if UIApplication.shared.canOpenURL(settingsURL) {
|
|
UIApplication.shared.open(settingsURL, options: [:], completionHandler: nil)
|
|
}
|
|
}
|
|
alertVC.addAction(cancelAction)
|
|
alertVC.addAction(setAction)
|
|
|
|
UIViewController.topMost?.present(alertVC, animated: true, completion: nil)
|
|
}
|
|
|
|
static func photosAuthorizationStatus(_ callback: @escaping Callback) {
|
|
PHPhotoLibrary.requestAuthorization { (status) in
|
|
switch status {
|
|
case .authorized:
|
|
DispatchQueue.main.async {
|
|
callback(true)
|
|
}
|
|
case .notDetermined, .denied, .restricted:
|
|
DispatchQueue.main.async {
|
|
callback(false)
|
|
}
|
|
default:
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
static func cameraAuthorizationStatus(_ callback: @escaping Callback) {
|
|
AVCaptureDevice.requestAccess(for: .video) { status in
|
|
DispatchQueue.main.async {
|
|
callback(status)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// CameraAuthHelper
|
|
typealias CameraEnableHandle = (_ enable: Bool, _ first: Bool) -> Void
|
|
class CameraAuth {
|
|
func cameraEnable(completion: @escaping CameraEnableHandle) {
|
|
guard UIImagePickerController.isSourceTypeAvailable(.camera) else {
|
|
completion(false, false)
|
|
return
|
|
}
|
|
let authStatus = AVCaptureDevice.authorizationStatus(for: .video)
|
|
if authStatus == .authorized {
|
|
completion(true, false)
|
|
} else if authStatus == .denied {
|
|
completion(false, false)
|
|
} else if authStatus == .restricted {
|
|
completion(false, false)
|
|
} else if authStatus == .notDetermined {
|
|
AVCaptureDevice.requestAccess(for: .video, completionHandler: { (firstState) in
|
|
completion(firstState, true)
|
|
})
|
|
}
|
|
}
|
|
|
|
func openSystemLight(open: Bool, device: AVCaptureDevice?) {
|
|
guard let device = device else {return}
|
|
if device.hasTorch {
|
|
do {
|
|
try device.lockForConfiguration()
|
|
device.torchMode = open ? .on : .off
|
|
device.unlockForConfiguration()
|
|
} catch {
|
|
print("电筒失败\(error)")
|
|
}
|
|
}
|
|
}
|
|
|
|
// 扫描image 二维码
|
|
func scanImageRQCode(image: UIImage) -> String? {
|
|
let ciImage: CIImage = CIImage(image: image)!
|
|
let context = CIContext(options: nil)
|
|
let detector = CIDetector(ofType: CIDetectorTypeQRCode,
|
|
context: context,
|
|
options: [CIDetectorAccuracy: CIDetectorAccuracyHigh])
|
|
let features = detector?.features(in: ciImage)
|
|
guard let results = features,
|
|
!results.isEmpty,
|
|
let qrText = results.first as? CIQRCodeFeature else { return nil}
|
|
return qrText.messageString ?? nil
|
|
}
|
|
}
|