Request Permission for Camera and Library in iOS 10 - Info.plist

徘徊边缘 提交于 2019-12-17 04:14:06

问题


I have implemented a WKWebView in an app. there's a file input in the shown web page where it should import an image from photos. Whenever i press on that input and select either "Take Photo" or "Photo Library" the app suddenly crash, which I believe is because the app is missing the permission to either take a photo or import from library.

How do I push a permission request when the user select one of the mentioned methods (Take Photo or Photo Library)?

I use Swift 3.0 with WKWebView.


回答1:


You have to add the below permission in Info.plist. More Referance

Camera :

Key       :  Privacy - Camera Usage Description   
Value     :  $(PRODUCT_NAME) camera use

Photo :

Key       :  Privacy - Photo Library Usage Description    
Value     :  $(PRODUCT_NAME) photo use



回答2:


You can also request for access programmatically, which I prefer because in most cases you need to know if you took the access or not.

Swift 4 update:

    //Camera
    AVCaptureDevice.requestAccess(for: AVMediaType.video) { response in
        if response {
            //access granted
        } else {

        }
    }

    //Photos
    let photos = PHPhotoLibrary.authorizationStatus()
    if photos == .notDetermined {
        PHPhotoLibrary.requestAuthorization({status in
            if status == .authorized{
                ...
            } else {}
        })
    }

You do not share code so I cannot be sure if this would be useful for you, but general speaking use it as a best practice.




回答3:


Swift 4

File: Info.plist

Camera

<key>NSCameraUsageDescription</key>
<string>camera description.</string>

Photos

<key>NSPhotoLibraryUsageDescription</key>
<string> photos description.</string>

Save Photos

  <key>NSPhotoLibraryAddUsageDescription</key>
  <string> photos add description.</string>

Location

<key> NSLocationWhenInUseUsageDescription</key>
<string> location description.</string>

Apple Music:

<key>NSAppleMusicUsageDescription</key>
<string>My description about why I need this capability</string>

Calendar

<key>NSCalendarsUsageDescription</key>
<string>My description about why I need this capability</string>

Siri

<key>NSSiriUsageDescription</key>
<string>My description about why I need this capability</string>



回答4:


Use the plist settings mentioned above and the appropriate accessor (AVCaptureDevice or PHPhotoLibrary), but also alert them and send them to settings if you really need this, like so:

Swift 4.0 and 4.1

func proceedWithCameraAccess(identifier: String){
    // handler in .requestAccess is needed to process user's answer to our request
    AVCaptureDevice.requestAccess(for: .video) { success in
      if success { // if request is granted (success is true)
        DispatchQueue.main.async {
          self.performSegue(withIdentifier: identifier, sender: nil)
        }
      } else { // if request is denied (success is false)
        // Create Alert
        let alert = UIAlertController(title: "Camera", message: "Camera access is absolutely necessary to use this app", preferredStyle: .alert)

        // Add "OK" Button to alert, pressing it will bring you to the settings app
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
          UIApplication.shared.open(URL(string: UIApplicationOpenSettingsURLString)!)
        }))
        // Show the alert with animation
        self.present(alert, animated: true)
      }
    }
  }



回答5:


File: Info.plist

For Camera:

<key>NSCameraUsageDescription</key>
<string>You can take photos to document your job.</string>

For Photo Library, you will want this one to allow app user to browse the photo library.

<key>NSPhotoLibraryUsageDescription</key>
<string>You can select photos to attach to reports.</string>



回答6:


Swift 4 The easiest way to add permissions without having to do it programatically, is to open your info.plist file and select the + next to Information Property list and scroll through the drop down list to the Privacy options and select Privacy Camera Usage Description for accessing camera or Privacy Photo Library Usage Description for accessing the Photo Library. Fill in the String value on the right after you've made your selection to include the text you would like displayed to your user when the alert pop up asks for permissions.




回答7:


To ask permission for the photo app you need to add this code (Swift 3):

PHPhotoLibrary.requestAuthorization({ 
       (newStatus) in 
         if newStatus ==  PHAuthorizationStatus.authorized { 
          /* do stuff here */ 
    } 
})


来源:https://stackoverflow.com/questions/39631256/request-permission-for-camera-and-library-in-ios-10-info-plist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!