70

iOS アプリを作成していますが、デバイスにカメラがあるかどうかを検出できるようにする必要があります。以前は、カメラが搭載されているのは iPhone だけだったので、デバイスが iPhone かどうかを確認していましたが、iPod Touch 4 の発売により、これは実行可能なオプションではなくなりました。アプリはカメラがなくても機能しますが、カメラがあると機能が追加されます。

では、カメラがあるかどうかを返すコードを誰かに教えてもらえますか?

4

8 に答える 8

168

+isSourceTypeAvailable:UIImagePickerControllerのメソッドを使用できます。

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
   // Has camera
于 2010-09-04T21:09:13.620 に答える
27

Juan Boeroが書いたように、次を確認してください。

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

しかし、Apple が PhotoPicker の例 ( PhotoPicker example Objective-C )で提案しているように、ユーザーがカメラへのアクセスを許可しているかどうかを確認する別のチェックを追加します。

* AVFoundation をインポートする必要があることに注意してください

スイフト 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
    }

スイフト 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)
}
于 2017-05-03T09:12:35.737 に答える
23

UIImagePickerController の代わりに AV Foundation クラスを使用している場合は、次のことができます。

BOOL hasCamera = ([[AVCaptureDevice devices] count] > 0);

UIImagePickerController を使用している場合は、プロジェクトに AVFoundation.framework を追加する必要があるため、おそらく価値がありません。

于 2014-01-22T10:58:56.240 に答える
20

はい、それを行うために提供されているAPIがあります。

BOOL isCamera = [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
于 2010-09-04T21:09:20.263 に答える
6

デバイスに前面カメラと背面カメラのどちらがあるかを特定する必要がある場合は、次を使用します。

isCameraAvailable = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
于 2014-07-04T06:51:48.667 に答える
-1

カメラのチェックが可能(Swift)

if(!UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
于 2016-08-30T04:13:35.643 に答える