Search code examples
ionic-frameworkionic3wkwebviewcontent-security-policy

window.Ionic.WebView.convertFileSrc not working in ionic 3


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


Solution

  • 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