WKWebview

So I noticed uiwebview was upgraded to WKWebview which is great, it’s much faster with the nitro JavaScript engine in iOS, although I have a problem I want to run a web page from the documents directory and it doesn’t seem to work, on investigation into the source code I see that when initializing the web view there is no configuration set to allow this, to make this work how can I get this added to the core source.

below is the code needed to be added to Rtt_IPhoneWebViewObject.mm at approx line 133

WKWebViewConfiguration \*theConfiguration = [[WKWebViewConfiguration alloc] init]; [theConfiguration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"]; // needed to run web page from documents directory [theConfiguration.preferences setValue:@YES forKey:@"OfflineApplicationCacheIsEnabled"]; // needed to run web page from documents directory // alternative initialization for web view fWebView = [[WKWebView alloc] initWithFrame:webViewRect configuration:theConfiguration]; 

What version of Corona are you using? I thought we fixed this.

Rob

2019.3562

if the source code is correct on github, then WKWebview isn’t configured for local files

I don’t see where this was even mentioned or fixed, uiwebview will load local files but you transitioned to WKWebview on the 22nd nov and you have to handle that differently, this is something I’ve dealt with a lot in native iOS. 

If you’re comfortable, you could pull down the source code, make the change, and after you test it, make a pull request to send your changes back up stream.

These are the kinds of things that the community can do to really help!

Rob

Unfortunately I’m not a big github user, we use Perforce for our version control, although I could give it a go I’m sure, but if somebody wants to help then I know what the change has to be

This is a breaking change for us - we need local HTML files as we make extensive use of them to display user-facing text from within our app.  Currently, we’re using Corona build 3552 as it’s the last version supporting UIWebView. However, there are important fixes ahead of this build (IAP stuff for example). 

Looks like it’s a trivial change to allow local HTML files in WKWebView.  Can engineering prioritize this?

+1!

Thanks.

Nah, they wont prioritize it. There are many more important things to do first.

Fair enough. What’s a solution in the interim? Presumably this is fixable with a plugin that wraps a WXWebView with the configuration in cublah’s original post? 

If someone can go through the process of what Rob suggested, the code posted above will work

This is where the file is

https://github.com/coronalabs/corona/blob/master/platform/iphone/Rtt_IPhoneWebViewObject.mm

You actually only need to add 3 lines

WKWebViewConfiguration \*theConfiguration = [[WKWebViewConfiguration alloc] init]; [theConfiguration.preferences setValue:@YES forKey:@"allowFileAccessFromFileURLs"]; // needed to run web page from documents directory // alternative initialization for web view fWebView = [[WKWebView alloc] initWithFrame:webViewRect configuration:theConfiguration]; 

Just replace line 134 with those 3 lines and it’s done

If that’s really all it takes, why not make a pull request?

As I said in a previous post git is not really something I know or use.

Ok I seemed to have done a pull request, I guess somebody needs to review it and merge it with the master branch

https://github.com/coronalabs/corona/pull/36

Thanks for this cublah! 

What’s involved in getting this tested and into a new Corona daily build? Is this something I can help with? 

I did some experiments with UIWebView (Corona 3552) vs WKWebView (3555).

I got some strange results loading a simple webpage CachesDirectory or ResourceDirectory. See here: https://imgur.com/a/CVCbLhY

UIWebView never fails to load, but WKWebView sometimes fails, depending upon iOS version.

I expect https://github.com/coronalabs/corona/pull/36 might fix some of these, but I don’t know how to build Corona from source and try this myself. 

In a separate test, I managed to get my web pages loading in a WKWebView, but only from ResourceDirectory, not CachesDirectory. CachesDirectory seems to be off-limits. This was with a much more complicate web page, with require.js, images, handbrake templates and other stuff - don’t know if this affects things, will need to test more. 

kernel sandbox deny(1) file-issue-extension target:/ class:com.apple.app-sandbox.read

This fix enables you to load local files from the documents directory, now it’s possible Apple have fixed this the the the last couple of versions of iOS but I know putting this in works on all versions, I’ve been using this in projects for the last 4 years and without it you can not read local files from the documents directory in all versions of iOS. I’m trying to get the fix added.

I have a semi-related WKWebView issue that I posted in another thread.  Anyone here have any insight?

https://forums.coronalabs.com/topic/77353-wkwebview-some-javascript-not-working/