Swift/iOS - 是否可以直接从 App 控制中心 运行 iOS 功能?

Swift/iOS - Is it possible to run iOS features from Control Center directly from the App?

我想知道是否可以直接从应用程序 运行 iOS 控制中心功能,例如屏幕录制或手电筒?如果是,怎么做?

使用下面的代码来使用手电筒,

func flashlight() {
    let flashLight: AVCaptureDevice? = AVCaptureDevice.default(for: .video)
    if flashLight?.isTorchAvailable() && flashLight?.isTorchModeSupported(.on) {
        let success: Bool? = try? flashLight?.lockForConfiguration()
        if success ?? false {
            if flashLight?.isTorchActive() != nil {
                flashLight?.torchMode = .off
            }
            else {
                flashLight?.torchMode = .on
            }
            flashLight?.unlockForConfiguration()
        }
    }
}

从iOS9开始,屏幕录制看起来像是可以使用ReplayKit来大大简化这一点。

func startRecording(_ sender: UIBarButtonItem, _ r: RPScreenRecorder) {

    r.startRecording(handler: { (error: Error?) -> Void in
        if error == nil { // Recording has started
            sender.title = "Stop"
        } else {
            // Handle error
            print(error?.localizedDescription ?? "Unknown error")
        }
    })
}

func stopRecording(_ sender: UIBarButtonItem, _ r: RPScreenRecorder) {
    r.stopRecording( handler: { previewViewController, error in

        sender.title = "Record"

        if let pvc = previewViewController {

            if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.pad {
                pvc.modalPresentationStyle = UIModalPresentationStyle.popover
                pvc.popoverPresentationController?.sourceRect = CGRect.zero
                pvc.popoverPresentationController?.sourceView = self.view
            }

            pvc.previewControllerDelegate = self
            self.present(pvc, animated: true, completion: nil)
        }
        else if let error = error {
            print(error.localizedDescription)
        }

    })
}

// MARK: RPPreviewViewControllerDelegate
func previewControllerDidFinish(_ previewController: RPPreviewViewController) {
    previewController.dismiss(animated: true, completion: nil)
}

有关详细信息,请访问此 link:https://developer.apple.com/reference/replaykit