標籤:lsp animate 自動跳轉 containe 麥克風 授權 eterm 相簿 跳轉
由於 iOS 系統的安全限制,App 如果需要訪問裝置的通訊錄、麥克風、 相簿、 相機、地理位置等時,需要請求使用者是否允許訪問。
有時使用者不小心點了“不允許”,後面可能就不知道要去哪裡再開啟這個許可權了。這就要求我們應用在每次調用相關功能的時候先擷取相關的授權狀態,如果還沒授權則彈出授權申請的提示框。如果之前被拒絕了,則彈出相關提示框讓使用者很方便地自動跳轉到設定頁面去修改許可權。
1,範例(1)這裡以照片的存取權限為例。為方便示範,我在頁面初始化完畢後就請求許可權。(2)第一次請求的時候我們選擇“不允許”。再次啟動程式時,會提示使用者照片訪問受限,需要授權。(3)點擊“設定”按鈕後便自動跳轉到系統設定頁面。
2,範例代碼
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
import UIKitimport Photos class ViewController: UIViewController { override func viewDidLoad() { _ = authorize() } func authorize()->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.authorize() }) }) 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: UIApplicationOpenSettingsURLString) 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) self.present(alertController, animated: true, completion: nil) }) } return false } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }} |
原文出自:www.hangge.com 轉載請保留原文連結:http://www.hangge.com/blog/cache/detail_1517.html
Swift - 判斷是否有某功能存取權限,沒有則提示,並自動跳轉到設定頁