Detect existence of camera in iPhone app?

前端 未结 8 765
逝去的感伤
逝去的感伤 2020-12-23 15:49

I\'m writing an iOS app, and I need to be able to detect if the device has a camera. Previously, I would check if the device is an iPhone or not, since only the iPhone has a

8条回答
  •  长情又很酷
    2020-12-23 16:47

    As Juan Boero wrote check the:

        if UIImagePickerController.isSourceTypeAvailable(.camera) {...}
    

    But I would add another check to see if the user allowed access to camera as apple suggests in their PhotoPicker example (PhotoPicker example Objective-C):

    *please note you have to import AVFoundation

    SWIFT 5

        let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
        switch authStatus {
            /*
             Status Restricted -
             The client is not authorized to access the hardware for the media type. The user cannot change the client's status, possibly due to active restrictions such as parental controls being in place.
             */
        case .denied, .restricted:
            // Denied access to camera
            // Explain that we need camera access and how to change it.
            let dialog = UIAlertController(title: "Unable to access the Camera", message: "To enable access, go to Settings > Privacy > Camera and turn on Camera access for this app.", preferredStyle: UIAlertController.Style.alert)
    
            let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil)
    
            dialog.addAction(okAction)
            self.present(dialog, animated:true, completion:nil)
        case .notDetermined:
            // The user has not yet been presented with the option to grant access to the camera hardware.
            // Ask for it.
            AVCaptureDevice.requestAccess(for: AVMediaType.video, completionHandler: { (grantd) in
            // If access was denied, we do not set the setup error message since access was just denied.
               if grantd {
               // Allowed access to camera, go ahead and present the UIImagePickerController.
                self.showImagePickerForSourceType(sourceType: UIImagePickerController.SourceType.camera)
                }
            })
        case .authorized:
            // Allowed access to camera, go ahead and present the UIImagePickerController.
            self.showImagePickerForSourceType(sourceType: UIImagePickerController.SourceType.camera)
        @unknown default:
            break; //handle other status
        }
    

    SWIFT 3

    let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
        
    if authStatus == AVAuthorizationStatus.denied {
        // Denied access to camera
        // Explain that we need camera access and how to change it.
        let dialog = UIAlertController(title: "Unable to access the Camera", message: "To enable access, go to Settings > Privacy > Camera and turn on Camera access for this app.", preferredStyle: UIAlertControllerStyle.alert)
            
        let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil)
            
        dialog.addAction(okAction)
        self.present(dialog, animated:true, completion:nil)
            
    } else if authStatus == AVAuthorizationStatus.notDetermined {     // The user has not yet been presented with the option to grant access to the camera hardware.
        // Ask for it.
        AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (grantd) in
        // If access was denied, we do not set the setup error message since access was just denied.
           if grantd {
           // Allowed access to camera, go ahead and present the UIImagePickerController.
                self.showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType.camera)
            }
        })
    } else {
            
        // Allowed access to camera, go ahead and present the UIImagePickerController.
        self.showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType.camera)
    
    }
    
    func showImagePickerForSourceType(sourceType: UIImagePickerControllerSourceType) {
        
        let myPickerController = UIImagePickerController()
        myPickerController.delegate = self;
        myPickerController.sourceType = sourceType  
        self.present(myPickerController, animated: true, completion: nil)
    }
    

提交回复
热议问题