2017-04-19 77 views
0

在我的應用程序在使用相機的實施如果用戶輸入的第一個意思是一次他們選擇允許他們訪問攝像頭和照片庫在整個應用程序的手段,如果選擇該公司否認意味着用戶無法訪問相機,因此我們需要在設置中更改相機和照片庫。之後,我們必須在我的情況下訪問相機,它不能正常工作,任何人都可以幫助我,非常感謝。問題與訪問照相機被拒絕後,相機

+0

再次要求允許 –

+0

我試過但我無法訪問相機和照片庫,請你提供一段代碼。 –

+0

@PatelJigar我認爲這是不可能要求兩次或更多的權限,當用戶不允許它第一次 –

回答

2

您可以先檢查一下相機的權限是否被允許。如果沒有,那麼你可以導航到設置屏幕&要求用戶許可。

override func viewDidLoad() { 
     super.viewDidLoad() 
     checkCameraPermission() 
    } 
    func checkCameraPermission() { 
     let cameraMediaType = AVMediaTypeVideo 
     AVCaptureDevice.requestAccess(forMediaType: cameraMediaType) { granted in 
      if granted { 
       //Do operation 
       print("Granted access for camera") 
       self.setCamera() 
      } else { 
       self.noCameraFound() 
       print("Denied access for camera ") 
      } 
     } 
    } 
    func noCameraFound(){ 
     let alert = UIAlertController(title: "AppName", message: "Please allow camera access in phone settings", preferredStyle: UIAlertControllerStyle.alert) 
     alert.addAction(UIAlertAction(title: "Back", style: UIAlertActionStyle.cancel, handler: {(action:UIAlertAction) in 


     })); 

     alert.addAction(UIAlertAction(title: "Open setting ", style: UIAlertActionStyle.default, handler: {(action:UIAlertAction) in 
      UIApplication.shared.open(NSURL(string:UIApplicationOpenSettingsURLString)! as URL, options: [:], completionHandler: nil) 

     })); 
     self.present(alert, animated: true, completion: nil) 
    } 
+0

謝謝傑克,真的很感激,讓我們檢查。 –