Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ionic 3 Camera not working in IPhone ios 11 #2020

Closed
mqasem92 opened this issue Oct 5, 2017 · 37 comments
Closed

Ionic 3 Camera not working in IPhone ios 11 #2020

mqasem92 opened this issue Oct 5, 2017 · 37 comments

Comments

@mqasem92
Copy link

mqasem92 commented Oct 5, 2017

I'm submitting a ... (check one with "x")
[ x] bug report
[ ] feature request

Current behavior:
I'm trying to build feature in my application that take photo from the camera and preview the taken photo in the <img [src]="" />

The camera'll open fine, but when try to preview the image nothing appear.

Notes:

  • It's working in the android devices
  • The preview shown when get photos from the mobile gallery.
  • I'm testing my app using Ionic View.

Expected behavior:
Preview the image taken from the Camera

Steps to reproduce:

Related code:

public photos: any = [];

constructor(public navCtrl: NavController,
    public navParams: NavParams,
    public actionSheetCtrl: ActionSheetController,
    private camera: Camera) {

  }

takePicture() {
    
    const options: CameraOptions = {
      quality: 100,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    }

    this.camera.getPicture(options).then((imageData) => {

      //add photo to the array of photos
      this.base64Image = "data:image/jpeg;base64," + imageData;
      this.addPhoto(this.base64Image);

    }, (error) => {
      console.debug("Unable to obtain picture: " + error, "app");
      console.log(error);
    });
  }

addPhoto(photo) {
      this.photos.push(photo);
      this.photos.reverse();
  }

 <ion-grid>
      <ion-row>
        <ion-col col-6 *ngFor="let photo of photos; let id = index">
          <ion-card>
            <img [src]="photo" *ngIf="photo" (click)="openPhotoActionSheet(id)" />
          </ion-card>
        </ion-col>
      </ion-row>
    </ion-grid>

Other information:
@ionic/cli-utils : 1.12.0
ionic (Ionic CLI) : 3.12.0

global packages:

cordova (Cordova CLI) : 7.0.1

local packages:

@ionic/app-scripts : 3.0.0
Cordova Platforms  : android 6.2.3 ios 4.4.0
Ionic Framework    : ionic-angular 3.7.0

System:

Node : v6.10.3
npm  : 3.10.10
OS   : Windows 10

Misc:

backend : legacy

package.json info:

{
    "name": "ionic-hello-world",
    "author": "mqasem",
    "homepage": "http://xxxx.com/",
    "private": true,
    "scripts": {
        "clean": "ionic-app-scripts clean",
        "build": "ionic-app-scripts build",
        "ionic:build": "ionic-app-scripts build",
        "ionic:serve": "ionic-app-scripts serve"
    },
    "dependencies": {
        "@angular/common": "^4.4.3",
        "@angular/compiler": "^4.4.3",
        "@angular/compiler-cli": "^4.4.3",
        "@angular/core": "^4.4.3",
        "@angular/forms": "^4.4.3",
        "@angular/http": "^4.4.3",
        "@angular/platform-browser": "^4.4.3",
        "@angular/platform-browser-dynamic": "^4.4.3",
        "@angular/platform-server": "4.0.2",
        "@ionic-native/camera": "^4.3.0",
        "@ionic-native/core": "^4.2.1",
        "@ionic-native/device": "^4.2.1",
        "@ionic-native/splash-screen": "^3.4.2",
        "@ionic-native/status-bar": "^3.4.2",
        "@ionic/storage": "^2.0.1",
        "@ngx-translate/core": "^8.0.0",
        "@ngx-translate/http-loader": "^2.0.0",
        "angular2-google-maps": "^0.17.0",
        "cordova-android": "^6.2.3",
        "cordova-ios": "^4.4.0",
        "cordova-plugin-camera": "^2.4.1",
        "cordova-plugin-compat": "^1.2.0",
        "cordova-plugin-console": "^1.0.5",
        "cordova-plugin-device": "^1.1.4",
        "cordova-plugin-ionic-webview": "^1.1.8",
        "cordova-plugin-splashscreen": "^4.0.3",
        "cordova-plugin-statusbar": "^2.2.1",
        "cordova-plugin-whitelist": "^1.3.1",
        "cordova-plugin-x-toast": "^2.5.2",
        "cordova-sqlite-storage": "^2.0.4",
        "ionic-angular": "^3.7.0",
        "ionic-native": "2.9.0",
        "ionic-plugin-keyboard": "^2.2.1",
        "ionicons": "3.0.0",
        "phonegap-plugin-barcodescanner": "^6.0.8",
        "rxjs": "^5.4.3",
        "sw-toolbox": "3.4.0",
        "zone.js": "^0.8.17"
    },
    "devDependencies": {
        "@ionic/app-scripts": "^3.0.0",
        "typescript": "~2.2.1"
    },
    "cordovaPlugins": [
        "cordova-plugin-whitelist",
        "cordova-plugin-console",
        "cordova-plugin-statusbar",
        "cordova-plugin-device",
        "cordova-plugin-splashscreen",
        "ionic-plugin-keyboard"
    ],
    "cordovaPlatforms": [],
    "description": "ionicTemplate2: An Ionic project",
    "cordova": {
        "plugins": {
            "cordova-plugin-console": {},
            "cordova-plugin-device": {},
            "cordova-plugin-splashscreen": {},
            "cordova-plugin-statusbar": {},
            "cordova-plugin-whitelist": {},
            "cordova-plugin-x-toast": {},
            "ionic-plugin-keyboard": {},
            "phonegap-plugin-barcodescanner": {
                "CAMERA_USAGE_DESCRIPTION": " "
            },
            "cordova-plugin-ionic-webview": {},
            "cordova-sqlite-storage": {},
            "cordova-plugin-compat": {}
        },
        "platforms": [
            "android",
            "ios"
        ]
    }
}
@UvealSnow
Copy link

I also have problems with this plugin, it works fine on Android but crashes the app on iOS11.

I can access the gallery/camera but when I select a picture the app will crash and exit

@rajaoui32
Copy link

Same here the app crashes on iOs 11 only after taking a picture

@mhartington
Copy link
Contributor

Anyone able to provide a sample of this? Im not able to recreate this in my tests

@rajaoui32
Copy link

I'm using Ionic 1 and I solved the issue by adding "Privacy- Photo Library Additions Usage Description" which is required by iOs 11

@mhartington
Copy link
Contributor

@mqasem92 and @UvealSnow can you both confirm if you have the privacy note added?

https://github.com/apache/cordova-plugin-camera#ios-quirks

@UvealSnow
Copy link

That would be in config.xml?

@rajaoui32
Copy link

No in the plist file in platforms/ios/"YOUR_APP_NAME"/

@mhartington
Copy link
Contributor

Config.xml or package.json

package.json

      "cordova-plugin-camera": {
        "CAMERA_USAGE_DESCRIPTION": " ",
        "PHOTOLIBRARY_USAGE_DESCRIPTION": " "
      }

config.xml

    <plugin name="cordova-plugin-camera" spec="^2.4.1">
        <variable name="CAMERA_USAGE_DESCRIPTION" value=" " />
        <variable name="PHOTOLIBRARY_USAGE_DESCRIPTION" value=" " />
    </plugin>

This will get copied over to your plist file at build time

@UvealSnow
Copy link

I didn't have them, but I added them and it still crashes after I select an image

@mhartington
Copy link
Contributor

Can you please provide a sample project.

@UvealSnow
Copy link

Ok so I made this small project, just added camera plugin and it does not crash, but I can´t get it to present the image, the code that I'm using is from another app I didn't in the past and it used to work fine until some days ago

https://github.com/UvealSnow/ionic-camera

@mhartington
Copy link
Contributor

@UvealSnow You're missing normalizeUrl on line 31

https://github.com/UvealSnow/ionic-camera/blob/master/src/pages/home/home.ts#L31

Because this is WKWebview, you need to rewrite the URLs to not include the file://

http://ionicframework.com/docs/wkwebview/#rewriting-file

@UvealSnow
Copy link

Thank you @mhartington I will cross check with my other project to see what's missing.
If you have time could you give me some tips on how to make a clean reinstall of the camera plugin?

@mhartington
Copy link
Contributor

ionic cordova plugin rm camera
ionic cordova plugin add cordova-plugin-camera  --variable CAMERA_USAGE_DESCRIPTION="your usage message" --variable PHOTOLIBRARY_USAGE_DESCRIPTION="your usage message"

@mqasem92
Copy link
Author

mqasem92 commented Oct 19, 2017

Great, it's working now.

What I did :

  • apply the following commands
ionic cordova plugin rm camera
ionic cordova plugin add cordova-plugin-camera  --variable CAMERA_USAGE_DESCRIPTION="your usage message" --variable PHOTOLIBRARY_USAGE_DESCRIPTION="your usage message"
  • This is my code

import { normalizeURL } from 'ionic-angular';

takePicture() {

    const options: CameraOptions = {
      quality: 100,
      destinationType: this.platform.is('ios') ? this.camera.DestinationType.FILE_URI : this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE
    }

    this.camera.getPicture(options).then((imageData) => {

      let base64Image = null;

      //get photo from the camera based on platform type
      if (this.platform.is('ios'))
        base64Image = normalizeURL(imageData);
      else
        base64Image = "data:image/jpeg;base64," + imageData;

      //add photo to the array of photos
      this.addPhoto(base64Image);

    }, (error) => {
      console.debug("Unable to obtain picture: " + error, "app");
      console.log(error);
    });
  }
openGallery() {
    let cameraOptions = {
      sourceType: this.camera.PictureSourceType.PHOTOLIBRARY,
      destinationType: this.camera.DestinationType.FILE_URI,
      quality: 100,
      targetWidth: 1000,
      targetHeight: 1000,
      encodingType: this.camera.EncodingType.JPEG,
      correctOrientation: true
    }

    this.camera.getPicture(cameraOptions).then((file_uri) => {

      //add photo to the array of photos
      this.addPhoto(normalizeURL(file_uri));

    }, (error) => {
      console.debug("Unable to obtain picture: " + error, "app");
      console.log(error);
    });
  }

@mhartington
Copy link
Contributor

🎉 Wonderful, glad to hear it's all worked out. Closing the issue then.

@glemiere
Copy link

glemiere commented Nov 7, 2017

I have the same problem here. This problems is caused by a bug from Apple (to change...) where safari seems to fail decoding base64 images. The reason why the code of @mqasem92 is working is because he's using FILE_URI source instead of DATA_URL.
So your code is a workaround but not a fix. I'll use it though.

@mhartington How do we fix it ? Any way to signal the problem to Apple ?

EDIT : I've opened a discussion on Apple's forum, you can follow it here --> https://forums.developer.apple.com/message/275994#275994

@Julsonic
Copy link

Hi Guys, is there any news on decoding base64 pictures using DATA_URL? On the Apple's forum I see that while there is silence..

@francarmona
Copy link

Same issue here when I use DATA_URL...

@tomNjerry
Copy link

getting same issue not getting displayed and crashing app wen selecting the image by using camera but images are not getting displayed even after using normalizeURL for ios

@glemiere
Copy link

glemiere commented Feb 6, 2018

@tomNjerry this problem sounds more like a usage description issue.
You should set NSCameraUsageDescription key. I'm sure it would solve your issue ;-)

@tomNjerry
Copy link

@glemiere Sir can you please brief me with an example as I am new with ionic Framework as of iOS is concerned. Because the same implementation done in android without applying normalizeURL of ionic-angular it just work fine that so i am Uploading multiple images. Sorry for late reply as I was out of station.
Thank you in advance

@erperejildo
Copy link

erperejildo commented Feb 25, 2018

it works perfectly if we add both description messages but it doesn't if we don't want to display anything. This is the solution that I found for that:
cordova plugin add cordova-plugin-camera --variable CAMERA_USAGE_DESCRIPTION=null --variable PHOTOLIBRARY_USAGE_DESCRIPTION=null --save

Edit: but even with this it fails sometimes (but not always, weird)

@glemiere
Copy link

Ha ha, I knew it.

@tomNjerry
Copy link

@erperejildo I did add CAMERA_USAGE_DESCRIPTION = "some msg" PHOTOLIBRARY_USAGE_DESCRIPTION = "some msg" but it still giving me the blank image, it is creating the image box for the uploaded image, as i am allowing user to upload multiple images. So the problem faced by me is image are getting upload but when i try to display the uploaded image it is giving the corresponding image box with no image displayed on it.

@erperejildo
Copy link

@Tommertom did you stopped the server and removed and added platform?

I just edited my answer. Even with that doesn't work 100% of the times. The problem is not from Ionic anyway, native people also face this problem sometimes (but it's easier to fix for them)

@glemiere
Copy link

glemiere commented Feb 26, 2018

@tomNjerry, @erperejildo

Solution:

  1. Open your Project/platforms/ios/Project.xcworkspace.

  2. In xCode, select your project and go to "Info" (like on the screenshot).

  3. Create a new row (the little "+" on my screenshot, it appears when your mouse is over a row) then enter "NSCameraUsageDescription" for the key, and whatever you want for the value. When you're done, repeat this for the key "NSPhotoLibraryUsageDescription".

screen shot 2018-02-26 at 17 57 48

Good to know:

If you run your project using xCode directly, you can search the console for "NS" and you will quickly find all the privacy descriptions that are missing for your app with the logged errors.

If you remove your platform you will have to perform this change again. It's not supposed to be a good practice using ionic, however in this case you should commit your info.plist to your repo, it might save you some time.

@erperejildo
Copy link

How can we do something similar for Android? (I know this is related to ios 11 but I was having same issue on Android)

@krunvaghela
Copy link

  1. Open "APP NAME"-info.plist File
  2. Privacy - Camera Usage Description : "Your Description"
  3. Privacy - Photo Library Additions Usage Description : "Your Description"

👍

@rambo14122
Copy link

rambo14122 commented Mar 8, 2018

Hi guys, I have seen the solution posted by @mqasem92 . The problem is I want DATA_URL instead of FILE_URI. I need to upload the image data to the server.
Any suggestion?

@mydoal
Copy link

mydoal commented Mar 16, 2018

Did anyone find a solution to the problem @rambo14122 is referring to?

@glemiere
Copy link

@mydoal https://forums.developer.apple.com/message/275994#275994
No solution until Apple debug Safari.

@glemiere
Copy link

@rambo14122 you should upload a file to your server for the moment instead of a data string. You can save it as a regular file on your server (remember the 10k subfolder limit on linux) and save the url in your database, or save it in your database as a blob.

Good luck!

@mhadiemami
Copy link

I had the same issue with the iOS 11 camera on and found this and it fix the problem right away:
https://www.npmjs.com/package/cordova-plugin-ios-camera-permissions

You can use the following code from cordova command-lind and it will fix the issue:

cordova plugin add cordova-plugin-ios-camera-permissions --variable CAMERA_USAGE_DESCRIPTION="your usage message" --variable PHOTOLIBRARY_ADD_USAGE_DESCRIPTION="your photo library usage message here" --variable PHOTOLIBRARY_USAGE_DESCRIPTION="your photo library usage message here" --save

@sergioChL
Copy link

nedd add lines to acces permision of camera and library into file of ionic config.xml

need camera access to take pictures need photo library access to get pictures from there need location access to find things nearby need photo library access to save pictures there

captura de pantalla 2018-09-07 a la s 12 43 34

@guths
Copy link

guths commented Mar 30, 2021

tnks a lot for this topic, help me a lot

@ppereira1402
Copy link

Anybody solve the problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests