JRBowl-Ish
JRBowl-Ish

Reputation: 11

Xamarin Forms, iOS will not open links in WebView

I have a Xamarin Forms application using webview to load an external web page into it. The web page has links that the application needs to access. Android opens the links just fine but iOS does not. I've tried using Device.OpenUri(new Uri(e.Url)); but that opens the link in Safari within the webview. I only want the link that I clicked on to open in the webview.

Upvotes: 0

Views: 3551

Answers (2)

Junior Jiang
Junior Jiang

Reputation: 12723

You can create a ContentPage which contained WebView , then when click button , it can navigate to a WebView.

Create a WebPage :

public class WebPage : ContentPage
{
    public string urlstr { get; set; }
    WebView browser;
    public WebPage()
    {
        browser = new WebView();
        Content = browser;
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        browser.Source = urlstr;
        browser.Reload();
    }
}

In Previous Page , invoking the clicked method :

private void Button_Clicked(object sender, EventArgs e)
{
    WebPage webPage = new WebPage();
    webPage.urlstr = "https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/webview?tabs=windows";
    Navigation.PushAsync(webPage);
}

================================Update===================================

Yeah ,this problem should be about App Transport Security in iOS, you can have a look at it .

While Apple highly suggests using the HTTPS protocol and secure communication to internet based information, there might be times that this isn't always possible. For example, if you are communicating with a 3rd party web service or using internet delivered ads in your app.

If your Xamarin.iOS app must make a request to an insecure domain, the following changes to your app's Info.plist file will disable the security defaults that ATS enforces for a given domain:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSAllowsArbitraryLoadsInWebContent</key>
     <true/>
</dict>

Upvotes: 0

JRBowl-Ish
JRBowl-Ish

Reputation: 11

Found the issue. My problem had to do with iOS not allowing website without https. For example if a link had google.com instead of https://google.com the app wouldn't allow it.

Upvotes: 1

Related Questions