Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use event onLoadResourceCustomScheme #365

Closed
rathanak opened this issue May 27, 2020 · 1 comment
Closed

How to use event onLoadResourceCustomScheme #365

rathanak opened this issue May 27, 2020 · 1 comment

Comments

@rathanak
Copy link

Hi ,
I got stuck with onLoadResourceCustomScheme event , how to declare it in webview option ? and how to call to use it in event ?

@pichillilorenzo
Copy link
Owner

Here is an example.

You need first declare what custom schemes you need to intercept using the resourceCustomSchemes option:

initialOptions: InAppWebViewGroupOptions(
    crossPlatform: InAppWebViewOptions(
      debuggingEnabled: true,
      resourceCustomSchemes: ["my-special-custom-scheme"]
    )
),

Then, in the onLoadResourceCustomScheme event you can intercept it and return your CustomSchemeResponse:

onLoadResourceCustomScheme: (InAppWebViewController controller, String scheme, String url) async {
  if (scheme == "my-special-custom-scheme") {
    var bytes = await rootBundle.load("test_assets/" + url.replaceFirst("my-special-custom-scheme://", "", 0));
    var response = CustomSchemeResponse(data: bytes.buffer.asUint8List(), contentType: "image/svg+xml", contentEnconding: "utf-8");
    return response;
  }
  return null;
},

In my example, I'm simply reading an asset file from the test_assets/ folder using rootBundle (import 'package:flutter/services.dart';) and return the new asset to the WebView.

In my test HTML I have:

<img id="image" src="my-special-custom-scheme://images/flutter-logo.svg" alt="flutter logo">

that will be intercepted by the onLoadResourceCustomScheme event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants