2

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="dev@cordova.apache.org" 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" />

dda
  • 6,030
  • 2
  • 25
  • 34
Josh
  • 23
  • 1
  • 5

1 Answers1

3

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?

Keerthi
  • 423
  • 3
  • 9
  • Added those into the Info.plist file and it did not work. But went into Xcode and added the new target to project and it now works. Thanks. – Josh Apr 09 '18 at 15:04
  • I have all the usage descriptions in my plist file, yet the iOS app still crashes. I have an app in production and it's been working fine before. For some messed up reason it doesn't work anymore. I open the library to choose a file or two, close the dialog, and the app crashes on subsequent requests. navigating away, pull-down to refresh etc. no errors no warning no nothing. the only thing I have is the crash log on my iPhone 8 which says the main thread was blocked for over 5sec and Watchdog Transgression was thrown. Any ideas? I've been struggling with this for two weeks now T_T – Nexus Dec 18 '18 at 04:01
  • @Nexus did it crashed before or after the camera appeared if so it could be the file permissions – zardilior Sep 06 '19 at 18:34