This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
WebView Navigation Can Now Be Cancelled Using A Task #12720 #12756
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Added a new property which is a Func that returns a Task<bool>. This can be implemented in the Navigating event fired by the WebView and will allow a developer to pass the native renderer some code to execute which will evaluate whether or not to cancel the current navigation. The WebView will wait for this code to execute before continuing.
Implemented the ability to cancel browser navigation using a task. The code here is not as elegant as iOS namely because of the ShouldOverrideUrlLoading method provided by android making it difficult to go to a background thread without using another method to serve as a fire and forget
Removing excess lines & moving methods around
This needs rebase on top of Main. |
Added a new property which is a Func that returns a Task<bool>. This can be implemented in the Navigating event fired by the WebView and will allow a developer to pass the native renderer some code to execute which will evaluate whether or not to cancel the current navigation. The WebView will wait for this code to execute before continuing.
Implemented the ability to cancel browser navigation using a task. The code here is not as elegant as iOS namely because of the ShouldOverrideUrlLoading method provided by android making it difficult to go to a background thread without using another method to serve as a fire and forget
Removing excess lines & moving methods around
@rmarinho I've rebased on main, I also marked the PR as draft because I want to gauge opinion on what I've proposed here 😁 |
@PureWeen is this similar to what you did for navigation in shell? should we use the same/similar approach ? |
@PureWeen I'll have a look at this PR and update mine accordingly 😄 Nice to know there is a pattern I can follow, I wasn't happy with how mine needed an extra property on the event args, when I was originally spitballing this code I was adding methods to set on the webview to invoke the function etc, quite nasty imho. |
I'm closing this PR as I've opened up a new one with the new implementation (deferral token) discussed here #14137 |
11 tasks
18 tasks
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issues Resolved
API Changes
Added:
public Func<Task<bool>> CancelTask { get; set; }
toWebNavigatingEventArgs
Platforms Affected
Behavioral/Visual Changes
By default there are no behavioural changes, developers would have to opt into this functionality.
I've added the ability for the
WebView
to cancel navigation asynchronously. In the current implementation theWebView
raisedWebNavigatingEventArgs
which containpublic bool Cancel { get; set; }
. The value is mutable and will be read by the control to determine whether or not to cancel navigation.From WkWebViewRenderer, Line 777-781:
As you can see if
args.Cancel
is mutated (in this case by the developer writing their forms app) the navigation will be cancelled. However if it takes too long for the developer to update theargs.Cancel
value then the native control will have already navigated. This becomes restrictive when trying to useXamarin.Forms
to build aWebView
based application as conditional access is an important feature.I have implemented this functionality on iOS and Android, I had raised an issue (#12720) but theres been no discussion on the issue so I've implemented what I think should be updated and want to see what peoples thoughts are.
Before/After Screenshots
Not applicable - No visual changes have been made & the behaviour is difficult to show in a gif.
Testing Procedure
iOS and Android are currently the only platforms affected.
I used the controls gallery app in the
Xamarin.Forms
solution to test my changes. I created a page with a standardWebView
and verified that theNavigating
event was being fired. I then wrote a method that would return whether or not I could navigate to a site (synchronously) and tested it was behaving as expected. I then wrapped that method in. a task (and ran aawait Task.Delay(2000)
) and ran that method, this allowed me to navigate to a site eventhough when my task completed it reported that navigation to site should be blocked. I implemented my code changes ensuring that when i had updated the code the original behaviour was the same as before I did the changes. I didn't check in any changes to the control app etc, I know the Xamarin Community Toolkit has a sample app where you can put your new feature / change.Once the code was complete I increased the delay on my async task and tested using the WebView. I ran the following test where the
facebook.com
domain was banned:https://google.co.uk
"facebook"
Google would load and work normally, when clicking through to facebook nothing would happen, I would see through debug logs that the site was being blocked. Whilst the task is running the UI thread is not blocked so the
WebView
can be interacted with until the task completes.PR Checklist