GauravSTomar
GauravSTomar

Reputation: 614

Check camera existence in ios phonegap

I want to check camera existence before showing the two source types when taking a picture in my phonegap application. For example, iPad 1 doesn't have an Camera, therefore I don't want to show the popup to select source type from Camera and My Photos. Is there something in phonegap that tell me camera exists in this device or not?

Upvotes: 3

Views: 3384

Answers (6)

Tom Kincaid
Tom Kincaid

Reputation: 4975

I needed to do just this, so I added it to a plug in that I made to do various tasks. Only iOS versions so far.

TomPhonegapUtility.h

#import <Foundation/Foundation.h>
#import <Cordova/CDV.h>
@interface TomPhonegapUtility : CDVPlugin
- (void) isCameraAvailable:(CDVInvokedUrlCommand*)command;
@end

TomPhonegapUtility.m

#import "TomPhonegapUtility.h"
#import <Cordova/CDV.h>
@implementation TomPhonegapUtility
- (void) isCameraAvailable:(CDVInvokedUrlCommand*)command {
    CDVPluginResult *pluginResult = nil;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:1];
    } else {
        pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsInt:0];
    }
    [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
@end

TomPhonegapUtility.js

function TomPhoneGapUtility () {
    this.isCameraAvailable = function(successCallback) {
        cordova.exec(successCallback, function(){}, "TomPhonegapUtility", "isCameraAvailable", []);
    }
}

How to use

var util = new TomPhoneGapUtility();
util.isCameraAvailable(function(hasCamera) {
    if (hasCamera) alert("YES");
    else alert("NO");
});

Upvotes: 1

Robert
Robert

Reputation: 302

Currently, there doesn't appear to be a way to query camera existence.

This is not ideal, but it could be a fail safe for you. If the device doesn't have a camera, a call to getPicture will fall into the fail handler, which returns a message. When the camera isn't available, that message is: "no camera available". So you could handle that failure once, then persist something in user settings, which you could query going forward. Like I said, not ideal. If the API can report this specific failure, it should also provide a way to check existence.

fail: function (message) { if (message == "no camera available") { // save this somewhere so next time we don't have to rely on the fail handler to tell us the camera doesn't exist } }

Upvotes: 1

websitemechanix
websitemechanix

Reputation: 45

I think this explains it well, if you haven't found the answer as of yet: http://docs.phonegap.com/en/1.4.1/phonegap_camera_camera.md.html#Camera

Upvotes: 0

Raymond
Raymond

Reputation: 751

I have done something like this when trying to use the HTML5 way of taking a picture :

if (typeof navigator.device !== 'undefined' && typeof navigator.device.capture !== 'undefined' && typeof navigator.device.capture.captureImage !== 'undefined') {
   // Can take a picture
} else {
   // No camera
}

Keep in mind that if you have used this solution or the previous one on the IOS simulator, it will give you a reliable answer, the simulator seem to return that it has a camera but is not able to simulate the taking of a picture and will return an error 20 ...

I am not happy with the if statement, if anybody know how to write this in a more logical way, please let me know, my JS knowhow is limited.

Upvotes: 0

user511530
user511530

Reputation: 191

Should be able to call this code to check for existence of a camera on any device.

if (typeof navigator.camera !== "undefined") { 
  // We are safe to use the camera 
} else { 
  // Bad news no camera 
} 

Upvotes: 0

nswamy
nswamy

Reputation: 1051

You can get the device model string from UIDevice class and check this

Upvotes: 0

Related Questions