-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Add prop + event for blacklisting URL-schemes in WebView #10654
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
Conversation
|
By analyzing the blame information on this pull request, we identified @jacobp100 and @spicyj to be potential reviewers. |
jacobp100
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like a good way to handle this.
React/Views/RCTWebView.m
Outdated
| }); | ||
|
|
||
| if (!isJSNavigation && _urlSchemeBlacklist) { | ||
| for (id scheme in _urlSchemeBlacklist) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use [_urlSchemeBlacklist containsObject:request.URL.scheme]?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure will do!
| } | ||
|
|
||
| @ReactProp(name = "urlSchemeBlacklist") | ||
| public void setUrlSchemeBlacklist(WebView view, @Nullable ReadableArray urlSchemeBlacklistArray) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this work when changing the urlBlacklistScheme from one non-null value to another non-null value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's from a non-null to another non-null, then the previous value will just be overwritten, right?
But, if you mean from a non-null value to a null value; yeah that would fail here. Will fix.
|
|
||
| /** | ||
| * An array defining blacklisted URL-schemes. | ||
| */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we call this something like rejectedUrlSchemes and onUrlSchemeRejected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I'm not a native English speaker I was quite unsure about naming here. If "rejected" sounds better I will change it! 👍
|
Is the above comment a showstopper @jacobp100? An alternative could be to refactor the Android |
|
It’s important that things work as a user would expect, and consistently between platforms, so this does sound like something that needs to be resolved. I’m not too familiar with Android, so I can’t really offer any help. Go with what you think is best! |
Probably a manual check can be added to handle this before setting the initial URL cc @javache |
|
@satya164 |
|
@jacobp100 I would say it would. Checking for schema is one of the things that one can achieve with my implementation |
|
@rseemann @jacobp100 I've commented in the other issue. |
|
I'm going through open PRs to see what can be merged. Looks like @10772 is still open, so I'm going to leave this one open as well. |
|
You might have to rebase on #10903 which just landed. |
| */ | ||
| scalesPageToFit: PropTypes.bool, | ||
|
|
||
| /** |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment doesn't add any info to the name onUrlSchemeRejected below.
Can you please explain what "rejected" means?
| onUrlSchemeRejected: PropTypes.func, | ||
|
|
||
| /** | ||
| * An array defining rejected URL-schemes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment doesn't add any info to the name rejectedUrlSchemes below.
Can you please explain what "rejected" means?
|
|
||
| private static String getUrlScheme(String url) { | ||
| if (url.contains("://")) | ||
| return url.split("://")[0]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm pretty sure there's a method for this in the SDK.
|
This seems similar to #10772. |
|
It is, the discussion has moved over to there to ensure that PR handles this use case. |
|
OK, it seems like we should close this PR in favor of #10772 then. |

Motivation
See #10055. As the issue says, a non-handled custom URL-scheme will throw an error on Android.
The motivation behind this PR is to make blocking URL-schemes more customizable.
In my specific use-case I do not want the URL to load, but I still need to intercept it. I've added a
rejectedUrlSchemesarray onWebView, which will let you set which schemes to block from loading, and aonUrlSchemeRejectedevent to notify you when an URL has been blocked.On iOS you can normally handle it by implementing
onShouldStartLoadWithRequest. But, what I've found is thatonShouldStartLoadWithRequestcan fail because it depends on an async callback from your JS code. If the code or device is a bit slow for whatever reason, it might not return true or false fast enough foronShouldStartLoadWithRequest.So, this PR is an attempt to instead provide a synchronous alternative for blocking certain URL-schemes on both Android and iOS.
Test plan
The above should blacklist
customschemeand block the above URL from loading. It should also log the given event which has anurland aschemeproperty.