Patching WebView plugin

Allow me to briefly explain what the pull request does to patch the webview plugin.

iOS:

The plugin uses the WKWebView class in native code. It calls the loadRequest method to invoke a request to the URI passed in from Flutter.

The WKWebView class provides another method loadFileURL that can load a file URL. The definition is:

In order to use this method, we need to know the URL of our asset file. We can find some hints in the Flutter online doc Sharing assets with the underlying platform. The example returns a path but we can use another method URLForResource to get the URL.

NSString* key = [registrar lookupKeyForAsset:@"icons/heart.png"];

NSString* path = [[NSBundle mainBundle] pathForResource:key ofType:nil];

With the current implementation, the FLTWebViewController and FLTWebViewFactory class in webview plugin don’t have access to registrar, so I changed their initWithMessenger method to initWithRegistrar. Now we have the answer to loading asset file in iOS webview:

NSString* key = [_registrar lookupKeyForAsset:url];

NSURL* nsUrl = [[NSBundle mainBundle] URLForResource:key withExtension:nil];

[_webView loadFileURL:nsUrl allowingReadAccessToURL:[NSURL URLWithString:@"file:///"]];

Android:

The webview plugin uses the WebView class on Android side and it calls the loadUrl method. This method already supports asset file URL in the following format:

file:///android_asset/{RELATIVE-PATH}

My first attempt was using “file:///android_asset/{MY-FOLDER}/{MY-FILE}.html” but it failed with File Not Found error.

I did some research and it turns out Flutter stores the user defined assets in a flutter_assets directory. So the answer to loading assets in Android webview is: