判斷相機及相簿存取功能

Henry Huang
6 min readJan 30, 2018

--

由於iOS對於安全性的要求,APP在使用如麥克風、定位、相機、相簿等功能時會要求開發者詢問使用者,而使用者如果點擊不允許的話,就有可能造成APP崩潰,而有些使用這並不知道該去哪裡修改設定,所以如果能判斷目前是隱私權是不允許就彈出提示框,讓使用者可以直接轉至APP要求權限設定頁面去做設定,也是不錯的體驗。

以要求相簿及相機權限為例:

宣告方法:

func authorize() -> Bool {  let photoLibraryStatus = PHPhotoLibrary.authorizationStatus() //相簿請求  let camStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) //相機請求  switch (camStatus, photoLibraryStatus){ //判斷狀態  case (.authorized,.authorized): //兩個都允許    return true  case (.notDetermined,.notDetermined): //兩個都還未決定,就請求授權   AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo,  completionHandler: { (status) in    DispatchQueue.main.async(execute: {      _ = self.authorize()    })})   PHPhotoLibrary.requestAuthorization({ (status) in     DispatchQueue.main.async(execute: {      _ = self.authorize()       })})   case (.authorized,.notDetermined): //相機允許,相簿未決定,相簿請求授權   PHPhotoLibrary.requestAuthorization({ (status) in    DispatchQueue.main.async(execute: {     _ = self.authorize()    })})   case (.authorized,.denied): //相機允許,相簿拒絕,做出提醒    let alertController = UIAlertController(title: "提醒❗️", message: "您目前拍攝的照片並不會儲存至相簿,要前往設定嗎?", preferredStyle: .alert)    let canceAlertion = UIAlertAction(title: "取消", style: .cancel, handler: {(status) in    UIImagePickerController.isSourceTypeAvailable(.camera)    let photoImagePC = UIImagePickerController()    photoImagePC.delegate = self    photoImagePC.sourceType = .camera    self.show(photoImagePC, sender: self)   })    let settingAction = UIAlertAction(title: "設定", style: .default, handler: { (action) in    let url = URL(string: UIApplicationOpenSettingsURLString)    if let url = url, UIApplication.shared.canOpenURL(url) {     if #available(iOS 10.0, *) {       UIApplication.shared.open(url, options: [:], completionHandler: { (success) in        print("跳至設定")        })    } else {        UIApplication.shared.openURL(url)      }    }})   alertController.addAction(canceAlertion)   alertController.addAction(settingAction)   self.present(alertController, animated: true, completion: nil)   default: //預設,如都不是以上狀態DispatchQueue.main.async(execute: {    let alertController = UIAlertController(title: "提醒❗️", message: "請點擊允許才可於APP內開啟相機及儲存至相簿", preferredStyle: .alert)    let canceAlertion = UIAlertAction(title: "取消", style: .cancel, handler: nil)    let settingAction = UIAlertAction(title: "設定", style: .default, handler: { (action) in    let url = URL(string: UIApplicationOpenSettingsURLString)    if let url = url, UIApplication.shared.canOpenURL(url) {      if #available(iOS 10.0, *) {    UIApplication.shared.open(url, options: [:], completionHandler: { (success) in    print("跳至設定")     })  } else {     UIApplication.shared.openURL(url)     }   }})   alertController.addAction(canceAlertion)   alertController.addAction(settingAction)   self.present(alertController, animated: true, completion: nil)     })   }    return false}}

此功能是參考此連結https://www.hangge.com/blog/cache/detail_1517.html

--

--

Henry Huang
Henry Huang

Written by Henry Huang

大學讀企管/退伍後在業務圈打滾6年後突然有一天決定投入完全沒接觸過的程式設計/目前為一位iOS Developer/熱愛NBA/dafu1231@gmail.com

No responses yet