apache / cordova-ios

Apache Cordova iOS
https://cordova.apache.org/
Apache License 2.0
2.15k stars 988 forks source link

Cordova not allowing https cross origin requests. #1082

Closed iliyaST closed 3 years ago

iliyaST commented 3 years ago

Issue Type

Description

Not able to make https requests (cross origin)

Information

Hello, When building app everything is ok, but after that I get some errors. Cross origin requests are only supported for HTTP. XMLHttpRequest cannot load file:///Users/usr/Library/Developer/CoreSimulator/Devices/A2CB28C4-7FC3-4103-9DA6-DF35D63C0D8C/data/Containers/Bundle/Application/3B8F3587-657F-454C-828A-95AAFD89CA9D/BreezeStaffApp.app/www/static/media/expect-good-news.1bfd09de.mp3 due to access control checks. Also added "< access origin="" /> < allow-navigation href="" /> < allow-intent href="*" />" From the documentation and nothing changed.

Command or Code

Cordova emulate iOS

Environment, Platform, Device

Xcode iOS emulator iOS 14.4 (iPhone 12 pro max)

Version information

Checklist

breautek commented 3 years ago

This is a limitation of the WKWebView which Cordova can't really do anything about. Fortunately there are some workarounds though.

You have three options:

A) For accessing local files, it's best to use the cordova-plugin-file to read the file, where you can read the file as binary and make blob urls to be used in the DOM. However if this isn't possible for whatever reason, such as a framework assumption, then option B or C should be work for you.

B) Use schemes. Schemes I don't think is documented yet, but it is explained in a blog post. All file:// requests are treated as a cross origin request because when on the file:// protocol, your origin is null. Schemes will allow your app to make app:// requests which wouldn't be considered cross origin. This will fix several other issues related to the app running on file:// in the WKWebView as well. Note that when using schemes, your origin will change. This will result in your app using a different sandbox containers for your web storage, cookies, indexed db, etc. If preserving this data is important for your app, then you'll need to find a plugin to do the data migration.

C) If you can't use schemes, then Oracle has a plugin that resolves XHR issues when using XHR on file:// urls. Try installing cordova-plugin-wkwebview-file-xhr. This approach will probbaly be the easiest to a working app but I would only recommend this approach as a last resort or a temporary measure. Migrating to schemes should probably be your goal.

Since this isn't a bug, I'll be closing this issue. If you need more assistance, I welcome you to our Slack community where you can ask more questions.

Kind Regards, Norman