Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

[url_launcher] Replace primary APIs with cleaner versions #5310

Merged
merged 17 commits into from
Apr 22, 2022

Conversation

stuartmorgan-g
Copy link
Contributor

This deprecates the existing APIs in favor of versions that:

  • Use Uri instead of String, to avoid a very common class of errors
  • Replace the platform-specific webview params with a platform-neutral LaunchMode option
  • Consolidate in-app webview configuration into a configuration object, and give it better defaults for web capabilities (since generally url_launcher is likely to be used as a browser, so should have browser-like defaults of supporting things like JavaScript)

See flutter/flutter#79043 for background.

New String-based versions (which are otherwise consistent with the Uri versions) are provided for a rare edge case we've seen in the past, but are separated into a separate file that is not exported to prevent accidental use due to autocomplete, since they are a foot-gun.

Also reworks the README, including:

  • Providing better guidance on use of canLaunchUrl that reflects the realities of platforms like web (and more generally, the fact that using canLaunchUrl+launchUrl is often worse than just launchUrl+fallback).
  • Reorganizing to group content better, and remove some redundancy.

Fixes flutter/flutter#79043
Closes flutter/flutter#51471

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/plugins repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the plugin surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is actually just a move of the old url_launcher.dart, unchanged.

@ditman
Copy link
Member

ditman commented Apr 21, 2022

This looks fantastic, let me do a proper review tomorrow!!

Copy link
Contributor

@bparrishMines bparrishMines left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with only a couple of comments.

* replace `forceSafariVC` and `forceWebView` with `LaunchMode`, which makes
the API platform-neutral, and standardizes the default behavior between
Android and iOS.
* move web view configuration options into a new `WebViewConfiguration`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is changing the default setting not a braking change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case the old API with the old config style is still available. You only need to migrate to the WebViewConfiguration if you start using the new URI/String based APIs (not the one under legacy)

Copy link
Member

@ditman ditman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the new approach very much, some minor comments/nits/praise below.

Comment on lines 127 to 129
For example, UI button that would have sent feedback email using a `mailto` URL
might instead open a web-based feedback form using an `https` URL on failure,
rather than disabling the button if `canLaunchUrl` returns false for `mailto`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent suggestion.

and thus no support for launching a URL using the `mailto` scheme.
In rare cases, you may need to launch a URL that the host system considers
valid, but cannot be expressed by `Uri`. For those cases, alternate APIs using
strings are available by importing `url_launcher_string.dart`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we take a page from the React book and call this file: dangerous_url_launcher_string.dart or similar, so users that import this get scared?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did think about it; my hope is that having to find a separate import is already enough of a deterrent.

If that turns out not to be true and we see a lot of people filing issue because they blindly used this and ignored all the warnings about how it should rarely be used, we could deprecate that import file and add one with a scary name.

@stuartmorgan-g stuartmorgan-g added the waiting for tree to go green (Use "autosubmit") This PR is approved and tested, but waiting for the tree to be green to land. label Apr 22, 2022
@fluttergithubbot fluttergithubbot merged commit ca63d96 into flutter:main Apr 22, 2022
mauricioluz pushed a commit to mauricioluz/plugins that referenced this pull request Jan 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p: url_launcher waiting for tree to go green (Use "autosubmit") This PR is approved and tested, but waiting for the tree to be green to land.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rework url_launcher API Flutter url_launcher canLaunch() method returning false on some (valid) web schemes
4 participants