开发者

How to determine if a camera flash is available on an iOS device

开发者 https://www.devze.com 2023-01-30 00:10 出处:网络
Overall my app works on all iOS device however I have a feature that only works on iPh开发者_JAVA技巧one 4\'s therefore if the user does not have an iPhone 4 I\'d like to display an alert letting them

Overall my app works on all iOS device however I have a feature that only works on iPh开发者_JAVA技巧one 4's therefore if the user does not have an iPhone 4 I'd like to display an alert letting them know that this feature won't work on their device?

How can I do that?

I'm trying to check if the device has the camera-flash


If you aren't using UIImagePicker:

AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
BOOL hasFlash = [device hasFlash];

There is also a [device hasTorch] if you need it.


Have you tried the various UIDevice methods? That said, there's most likely an easier way, depending on what specific feature your optional functionality requires, but without knowing what this is it's hard to give a concrete example.

N.B.: I'd also recommend not presuming a feature set based on the detected device itself for reasons of future compatibility.

UPDATE

In terms of the camera flash, I know you can use the UIImagePickerController's isFlashAvailableForCameraDevice: method. (You'll probably want to call this after first verifying the existance of a camera via the isCameraDeviceAvailable: method.)


You don't need to know what device you're running on, check to see if the feature you want to use is available and display an alert if it isn't.

If you let us know what specific feature you're looking for, we can help with the specific way to check for that feature.

Though, my own advice here, popping up a dialog box that says, "If you had a newer phone, you'd be able to do this!" is really just rubbing your user's face in the fact that they've got an older phone, now isn't it? A better way to handle it would be to 1) make it clear in your AppStore page that certain features won't be available on non iPhone 4 phones and 2) make your app's behavior degrade gracefully on non iPhone 4s.


For swift 4.0

let device = AVCaptureDevice.default(for: .video)
         var hasFlash: Bool! = false
         hasFlash = device?.hasFlash


This question is a little dusty but if you come across it, here is some current information.

First, you can require that the device have a flash via the following value for UIRequiredDeviceCapabilities in your info.plist file.

camera-flash

Include this key if your app requires (or specifically prohibits) the presence of a camera flash for taking pictures or shooting video. Apps use the UIImagePickerController interface to control the enabling of this feature.

Documentation link

Next, you can check your UIImagePickerController to see if one is available. This is useful as not every camera on every device has a flash.

isFlashAvailable(for:)

Indicates whether a given camera has flash illumination capability.

Documentation link

Or, if you are using AVFoundation, you can query the AVCaptureDevice:

captureDevice.hasFlash

Indicates whether the capture device has a flash.

Documentation link

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号