Reputation: 5972
Overall my app works on all iOS device however I have a feature that only works on iPhone 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
Upvotes: 6
Views: 6007
Reputation: 1736
Solution in Objective-C:
- (bool)hasDeviceLED {
bool hasFlash = NO;
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType: AVMediaTypeVideo];
if (device != nil) {
hasFlash = device.hasFlash;
}
return hasFlash;
}
Successfully tested with iPhone and iPad.
Upvotes: -1
Reputation: 1990
For swift 4.0
let device = AVCaptureDevice.default(for: .video)
var hasFlash: Bool! = false
hasFlash = device?.hasFlash
Upvotes: 2
Reputation: 36620
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.
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.
Or, if you are using AVFoundation
, you can query the AVCaptureDevice
:
captureDevice.hasFlash
Indicates whether the capture device has a flash.
Upvotes: 0
Reputation: 752
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.
Upvotes: 11
Reputation: 54445
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.)
Upvotes: 8
Reputation: 49354
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.
Upvotes: 3