3

I'm building a mobile app using WKWebView. I register custom url schemes image:// and thumb:// to serve assets from the native part to the web part of the app.

webViewConfiguration.setURLSchemeHandler(handler, forURLScheme: "image")
webViewConfiguration.setURLSchemeHandler(handler, forURLScheme: "thumb")

This approach works well if urls with a custom scheme are used in HTML. For example, <img src="thumb://watermarkly.com/1.jpg" /> works properly - WKWebView invokes my handler and displays the result. However, WKWebView blocks requests if I try to fetch these urls using XMLHttpRequest:

[Warning] The page at https://watermarkly.com/app/watermark/ was allowed to display insecure content from thumb://watermarkly.com/1.jpg.

[Warning] [blocked] The page at https://watermarkly.com/app/watermark/ was not allowed to display insecure content from image://watermarkly.com/1.jpg.

[Error] Not allowed to request resource

[Error] XMLHttpRequest cannot load image://watermarkly.com/1.jpg due to access control checks.

The only difference here is that "thumb:" url was assigned to an img tag, while "image:" url were fetched via XMLHttpRequest. Unfortunately, no other info provided in Safari Developer Tools.

The problem appears on a real devices only - everything works properly in iOS Simulator.

Is there something I need to configure to make it work for XMLHttpRequests as well?

Update

We switched from HTTPS to HTTP to make XHR to solve the problem.

Unfortunately, custom url schemes seems not to work on some iPhones. We have 5 customers with iPhones where nor switching to HTTP, nor sending Access-Control-Allow-Origin header help. We weren't able to identify which setting causes the issue - the problem cannot be reproduced on any of devices we have. Apple reviewer didn't have any complaints as well. Nor XHR, not getting images through urls work on these phones. One of the customers has two phones. Custom url schemes work on one of them and they don't work on the second one at all. He says they are identical and there is no Safari extensions installed. Unfortunately, we weren't able to identify what causes the problem. Beware custom url schemes may not work on some phones.

Community
  • 1
  • 1
Ivan Nikitin
  • 3,578
  • 27
  • 39
  • Did you ever find a solution or work around. Having similar problem trying to replace UIWebView logic with WKWebView. – David Ingham Jun 02 '20 at 16:27
  • @DavidIngham Unfortunately, only to use http. This solves the problem on all phones we have, but some clients still complaint and we see crash log telling the custom scheme url cannot be open. – Ivan Nikitin Jun 02 '20 at 17:44
  • @DavidIngham were you able to identify what causes the problem on your phones? Probably some configuration setting? – Ivan Nikitin Jun 02 '20 at 18:02
  • @DavidIngham update: there are a lot of problematic phones. – Ivan Nikitin Jun 04 '20 at 16:51
  • Yeah I found the solution for my issue which was similar. Pages and resources would serve by gets etc but not via xhr. They always were treated as an error. Turns out I was using NSUrlReponse but for the xhr stuff I needed to switch over to NSHttpUrlResponse and send the Access-Control-Allow-Origin headers. After converting to those and sending the usual headers content type and length etc it started working and xhr stopped treating them as errors – David Ingham Jun 05 '20 at 17:38
  • @DavidIngham unfortunately, it isn't suffice on all iPhones. We have 5 customers with iPhones where Acess-Control-Allow-Origin doesn't help. We weren't able to identify which setting causes the issue - the problem cannot be reproduced on any of devices we have. Apple reviewer didn't have any complaints as well. Getting images through urls also didn't work on these phones. One of the customers has two phones. Custom url schemes work on one of them and they don't work on the second one. He says they are identical and there is no Safari extensions installed. Beware custom url schemes may not work – Ivan Nikitin Jun 08 '20 at 11:45

0 Answers0