0

I am trying to display image selected from camera/gallery using cordova-plugin-camera plugin and used window.Ionic.WebView.convertFileSrc() for Rewriting file:// due to wkwebview.

It works fine in android platform but in iOS platform it gives error for Content Security Policy

For example I am getting file:///Users/CN339/Library/Developer/CoreSimulator/Devices/XXXXXXX/data/Containers/Data/Application/XXXXX/Library/NoCloud/cdv_photo_002.jpg from camera plugin and after using window.Ionic.WebView.convertFileSrc on previous local url I am getting below error.

Content Security Policy error

Kandy
  • 673
  • 9
  • 21
Vinay Thakur
  • 3
  • 1
  • 3
  • 1
    You can use normalize: https://ionicframework.com/docs/3.2.0/api/util/normalizeURL/ – Sjoerd de Wit Jan 17 '19 at 12:36
  • Thanks for the reply. I used it also but it is showing deprecated, recommends to use window.Ionic.WebView.convertFileSrc instead. and it is not working as well – Vinay Thakur Jan 17 '19 at 12:39
  • @VinayThakur Have u found the solution? – Abdu4 Apr 24 '19 at 05:55
  • @Abdu4 Yes. You have to use window.Ionic.WebView.convertFileSrc along with Angular's DomSanitizer bypassSecurityTrustUrl. – Vinay Thakur Apr 26 '19 at 06:34
  • @VinayThakur can you please explain in detail how to use DomSanitizer bypassSecurityTrustUrl with use window.Ionic.WebView.convertFileSrc – Karthik Apr 26 '19 at 08:06
  • @VinayThakur Can you add the codes as the answer, then you can make it as the accepted answer and of course we can upvote the answer as well. – Abdu4 Apr 27 '19 at 05:21

3 Answers3

2

I tried a lot of methods to load local files such images or videos. I tried using

  • normalizeUrl (from ionic-angular)
  • regex and replace the local file path: path.replace(/^file:///, '')
  • window.Ionic.WebView.convertFileSrc()

None of above worked for me. The only way I could load a local file was using

window['Ionic']['WebView'].convertFileSrc(path);

Hope it works as you want. I have only tested it on Android

UPDATE You can use win.Ionic.WebView.convertFileSrc(), like third bullet, only if you declare win: any = window. To define win as any did the trick for me - https://stackoverflow.com/a/55934321/10243902

Mati Cassanelli
  • 625
  • 6
  • 11
0

Just had the same problem on iOS after ionic-webview suddenly stopped working, so I uninstalled and reinstalled the plugin:

cordova plugin remove cordova-plugin-ionic-webview

cordova plugin add cordova-plugin-ionic-webview

window.Ionic.WebView.convertFileSrc started working like a charm again.

Dinesh Gurjar
  • 498
  • 5
  • 17
-1

this.tempSchemeImagePath = window['Ionic']['WebView'].convertFileSrc(nativeURL);