I'm trying to use the Cordova camera plugin in an application. It works fine on Android devices, but for some reason crashes the app when installed on iOS devices. When the camera is invoked in the iOS emulator, it fails.(Because the emulator obviously doesn't have a camera). Are there any solutions to this issue?
HTML
<!--Save my look-->
<div id="saveMyLook">
<div id="cameraSection">
<button id="cameraBtn">Lets take a picture</button>
<img id="myImage" width="35%" height="35%" />
</div>
</div>
Javascript
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log(navigator.camera);
}
var camBtn=document.getElementById("cameraBtn");
function cam (){
navigator.camera.getPicture(onSuccess, onFail,
{sourceType: Camera.PictureSourceType.CAMERA});
function onSuccess(imageURI) {
var image = document.getElementById('myImage');
image.src = imageURI;
}
function onFail(message) {
alert( message);
}
}
camBtn.addEventListener('click',cam);
Config.xml
<?xml version='1.0' encoding='utf-8'?>
<widget id="com.MakeMePretty.joshua" version="1.0.0"
xmlns="http://www.w3.org/ns/widgets"
xmlns:cdv="http://cordova.apache.org/ns/1.0">
<name>MakeMePretty</name>
<description>
A sample Apache Cordova application that responds to the
deviceready event.
</description>
<author email="[email protected]" href="http://cordova.io">
Apache Cordova Team
</author>
<content src="index.html" />
<plugin name="cordova-plugin-whitelist" spec="1" />
<access origin="*" />
<allow-intent href="http://*/*" />
<allow-intent href="https://*/*" />
<allow-intent href="tel:*" />
<allow-intent href="sms:*" />
<allow-intent href="mailto:*" />
<allow-intent href="geo:*" />
<platform name="android">
<allow-intent href="market:*" />
</platform>
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
</platform>
<plugin name="cordova-plugin-camera" spec="^4.0.2">
<variable name="CAMERA_USAGE_DESCRIPTION" value="Access Camera" />
<variable name="PHOTOLIBRARY_USAGE_DESCRIPTION" value="Need to
access photolibrary" />
</plugin>
<engine name="ios" spec="^4.5.4" />
<engine name="android" spec="^7.0.0" />
Please add the below lines to your Info.plist as source code:
<key>NSCameraUsageDescription</key> <string>Camera usage description</string>
This is because after iOS 10 you have to define and provide a usage description of all the system’s privacy-sensitive data accessed by your app in Info.plist.
More detailed explanation can be found as replies to this question: NSCameraUsageDescription in iOS 10.0 runtime crash?