let status =AVCaptureDevice.authorizationStatus(for: .video)
if status == .authorized { // connect to video device }
if status == .denied { // show error }
// 如果需要,使用 requestAccess(for:completionHandler:) 请求权限 AVCaptureDevice.requestAccess(for: .video) { (accessGranted) in // handle result }
请求辅助功能权限
1 2 3 4 5 6 7 8
let key = kAXTrustedCheckOptionPrompt.takeRetainedValue() asString if (!AXIsProcessTrustedWithOptions([key: true] asCFDictionary)) { // Sometimes Prompt option above doesn't work, actually trying to send key may open that dialog. let src =CGEventSource(stateID: .hidSystemState) // "Fn" key down and up CGEvent(keyboardEventSource: src, virtualKey: 63, keyDown: true)?.post(tap: .cghidEventTap) CGEvent(keyboardEventSource: src, virtualKey: 63, keyDown: false)?.post(tap: .cghidEventTap) }
延迟执行
1 2 3 4 5 6 7
// 阻塞当前线程 sleep(5)
// 新建一个线程 DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() +5) { // do something }