Native camera plugin is crashing app - on iPhone 7, 6, 5 , iOS - 11.1.2

Hi Folks,

My app is crashing each time I try to open the device camera, It’s only happening in iOS device but works fine android.

Ionic info -

cli packages: (/usr/local/lib/node_modules)

    @ionic/cli-utils  : 1.19.0
    ionic (Ionic CLI) : 3.19.0

global packages:

    cordova (Cordova CLI) : 7.1.0 

local packages:

    @ionic/app-scripts : 3.1.2
    Cordova Platforms  : ios 4.5.3
    Ionic Framework    : ionic-angular 3.9.2

System:

    ios-deploy : 1.9.2 
    Node       : v6.10.0
    npm        : 3.10.10 
    OS         : macOS Sierra
    Xcode      : Xcode 9.1 Build version 9B55 

Environment Variables:

    ANDROID_HOME : not set

Misc:

    backend : pro

Stuck with it pls help.

Wich plugin are you using to access the camera? And which version of this plugin?

It’s - cordova-plugin-camera@3.0.0

Good. Have you defined the mandatory edit-config tags in your config.xml?

If not, see chapter iOS Quirks of https://github.com/apache/cordova-plugin-camera

1 Like

@reedrichards - thanks for the link. But alone that didn’t solve my problem. I needed to add targetWidth and targetHeight into options. Now it’s work like a charm.

Cheers!

openDeviceCamera() {
    const options: CameraOptions = {
      quality: 100,
      targetWidth: 900,
      targetHeight: 600,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      saveToPhotoAlbum: false,
      allowEdit: true,
      sourceType: 1
    }
    this.camera.getPicture(options).then((imageData) => {
      this.imgbase64 = 'data:image/jpeg;base64,' + imageData;
    }, (err) => {
      alert("Failed to capture image");
    });

  }
7 Likes

Awesome, congratulations for finding the solution!

What @champion007 says is right, but in my case, I already had that, so I am here to basically confirm @reedrichards solution DOES work. If you want to add it into the config.xml then code looks like this:

<edit-config target="NSCameraUsageDescription" file="*-Info.plist" mode="merge">
    <string>need camera access to take pictures</string>
</edit-config>
<edit-config target="NSPhotoLibraryUsageDescription" file="*-Info.plist" mode="merge">
    <string>need photo library access to get pictures from there</string>
</edit-config>
<edit-config target="NSLocationWhenInUseUsageDescription" file="*-Info.plist" mode="merge">
    <string>need location access to find things nearby</string>
</edit-config>
<edit-config target="NSPhotoLibraryAddUsageDescription" file="*-Info.plist" mode="merge">
    <string>need photo library access to save pictures there</string>
</edit-config>

and if you prefer going into the -Info.plist file then:

<key>NSCameraUsageDescription</key>
<string>need camera access to take pictures</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>need photo library access to get pictures from there</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>need location access to find things nearby</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>need photo library access to save pictures there</string>

Thanks again, the both of you!

5 Likes

The solution works perfect for IOS, but when built for android the code in config.xml throws an error
“Cannot read property ‘find’ of undefined”. Not sure if it was where I placed the code, but I removed the code block and the build ran fine after that. Just wanted to see if I may have done something wrong, or something is breaking in the code itself.

Thanks!

@stemsoftware Well, here’s what I think. Just off the top pf my head, because I built my app to work on iOS to be quite frank, I didn’t quite care much about android (yet). Having said that in your config.xml file you have two major sections:

<platform name="android">
...
...
</platform>
<platform name="ios">
...
...
</platform>

I don’t believe the exact same code works for Android, but there most likely is an equivalent. Either way, it should be inside the platform tag with the android property and your iOS related code inside the iOS platform tag. Does that make sense?

Thank u so much, u saved my time. It works perfectly.

Hi,
Camera is not working in my app for ios but working in android.I have used same process but it is not working.
Please help.

Ionic:

ionic (Ionic CLI) : 4.6.0 (/usr/local/lib/node_modules/ionic)
Ionic Framework : ionic-angular 3.9.4
@ionic/app-scripts : 3.2.4

Cordova:

cordova (Cordova CLI) : 8.1.2 (cordova-lib@8.1.1)
Cordova Platforms : android 7.1.4, browser 5.0.4, ios 4.5.5
Cordova Plugins : cordova-plugin-ionic-keyboard 2.1.3, cordova-plugin-ionic-webview 4.1.1, (and 12 other plugins)

System:

ios-deploy : 1.9.4
NodeJS : v10.15.0 (/usr/local/bin/node)
npm : 3.10.10
OS : macOS High Sierra
Xcode : Xcode 10.1 Build version 10B61