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

Fix #1350, Handling the dart uri parser's problem(cannot parse uri including ':') #1403

Closed
wants to merge 1 commit into from

Conversation

Woojin-Crive
Copy link

@Woojin-Crive Woojin-Crive commented Oct 27, 2022

Connection with issue(s)

Resolve issue #1350

Purpose

Adding onShouldOverrideUrlLoadingFailedToParseUri to handle the dart uri parser's problem(cannot parse uri including ':').

Example

I made a method channel and passed the original url to native.
kotlin, swift will handle the original url when the exception is raied.

InAppWebView(
    onShouldOverrideUrlLoadingFailedToParseUri: (controller, url) async {
      if (Platform.isAndroid) {
        await AndroidMethod.onShouldOverrideUrlLoadingFailedToParseUri(url: url);
        return;
      }
      if (Platform.isIOS) {
        await IosMethod.UIApplicationSharedOpen(url: url);
        return;
      }
    },
    shouldOverrideUrlLoading: (controller, navigationAction) async {
      Uri uri = navigationAction.request.url!;
      
      if (uri.scheme.toLowerCase() == "shouldOverrideUrlLoading") {
        return NavigationActionPolicy.CANCEL;
      }
      ....
    },
)

@pichillilorenzo
Copy link
Owner

pichillilorenzo commented Oct 27, 2022

Thanks for trying to solve that issue but this kind of pull request seems a workaround instead of a solution.
I'm already working on a solution that, unfortunately, would require updating the plugin to version 6.x.x (it should be available with the next version 6.0.0-beta.10)!

What I'm doing now is creating a new class called WebUri that implements Uri dart interface but also maintains the raw string value in case an issue like this happens, so the raw string value could be used as a fallback value.

Also, WebUri still be considered a Uri.

This is a big breaking change so, I can't add it to version 5.x.x.

This is a preview:

class WebUri implements Uri {
  // ...

  WebUri(String source, {this.forceToStringRawValue = false}) {
    this._rawValue = source;
    try {
      _uri = Uri.parse(this._rawValue);
      this._isValidUri = true;
    } catch (e, stacktrace) {
      print(e);
      print(stacktrace);
    }
  }

  // ...
}

// InAppWebView example
InAppWebView(
  initialUrlRequest:
    URLRequest(url: WebUri('https://flutter.dev'))
)

// example of letter case difference
final uri = WebUri('scheme://customHostValue', forceToStringRawValue: false);
print(uri.rawValue); // scheme://customHostValue
print(uri.isValidUri); // true
print(uri.uriValue.toString()); // scheme://customhostvalue
print(uri.toString()); // scheme://customhostvalue

uri.forceToStringRawValue = true;
print(uri.toString()); // scheme://customHostValue

// example of a not valid URI
// Uncaught Error: FormatException: Invalid port (at character 14)
final invalidUri = WebUri('intent://not:valid_uri');
print(invalidUri.rawValue); // intent://not:valid_uri
print(invalidUri.isValidUri); // false
print(invalidUri.uriValue.toString()); // ''
print(invalidUri.toString()); // intent://not:valid_uri

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

Successfully merging this pull request may close these issues.

2 participants