1

When opening a link to a website from my WebView both with Android and iOS I'm able to do it with the default browser of the phone. However, when going back to the app, with Android I see the expected and desired behavior of seeing the original WebView page, instead on iOS I see the opened website inside the application. Here the GitHub link to reproduce the behavior: https://github.com/irdalan/WebViewTestApp

Behavior for Android: From left to right:

  1. AndroidApp before clicking on the link 2. Navigating to link with default browser on Android 3. AndroidApp after going back from browser to App.

AndroidApp before clicking on the link Navigating to link with default browser on Android AndroidApp after going back from browser to App

Behavior for iOS: From left to right:

  1. IphoneApp before clicking on the link 5. Navigating to link with default browser on iOS 6. IphoneApp after going back from browser to App.

IphoneApp before clicking on the link Navigating to link with default browser on iOS IphoneApp after going back from browser to App

EDIT: Code behind of webview page

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace WebViewTestApp
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            HtmlWebViewSource localhtml = new HtmlWebViewSource();


            string text = "<html><body><h1>Xamarin.Forms</h1><p>Welcome to WebView.<a href= \"https://en.wikipedia.org/wiki/Xamarin\">Xamarin</a></p></body></html>";

           
            localhtml.Html = text.Replace(@"\", string.Empty);
            _webview.Source = localhtml;
            _webview.Navigating += async (s, e) =>
            {
                if (e.Url.StartsWith("http"))
                {
                    try
                    {
                        var uri = new Uri(e.Url);
                        await Launcher.OpenAsync(uri);
                    }
                    catch (Exception ex)
                    {

                    }
                    finally
                    {
                        e.Cancel = true;
                    }

                }
            };
        }
    }
}
1
  • Its great that you have a link to a repo! But please also add the most relevant code directly in the question. This ensures that the question is self-contained (links can break over the years), and makes it easier to zero-in on the most important code. Commented Apr 13, 2022 at 16:21

1 Answer 1

1

Try delaying the Launcher call, so that Cancel of Navigating happens before switch to browser happens. This should suppress the default behavior, which is what displayed the web page w/i webview itself.

Change:

await Launcher.OpenASync(uri);

To:

Device.BeginInvokeOnMainThread(async () =>
    await Launcher.OpenAsync(uri));

OPTIONAL:

To make it clear that you want Cancel to happen first, you could move e.Cancel = true; before the Launcher line. However, this isn't strictly necessary: what's inside BeginInvoke won't get executed until your Navigating method returns.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.