How to Open Default Browser in Xamarin?

I would like to open a web page in Xamarin for both Android and iOS but the code wouldn't work. Can anyone help me figure out what's wrong?

For Android, clicking the button goes through the code, but nothing happens afterward. For iOS, clicking the button does open a Safari page, but it cannot open the page, and displays a message "Safari could not open the page because the server stopped responding."

The test arg passed is: ""

Thanks.

Code:

    async Task ExecuteLaunchBrowserAsync(string arg)
    {
        if (IsBusy)
            return;

        try
        {
            await CrossShare.Current.OpenBrowser(arg, new BrowserOptions
            {
                ChromeShowTitle = true,
                ChromeToolbarColor = new ShareColor
                {
                    A = 255,
                    R = 100,
                    G = 50,
                    B = 255
                },
                UseSafariReaderMode = true,
                UseSafariWebViewController = true
            });
        }
        catch
        {
        }
    }
2

2 Answers

You just need to use blow code, it will work for both Android and iOS.

void Device.OpenUri(Uri uri)

eg.

Device.OpenUri(new Uri(""))

To work in iOS use Transport security in Info.plist

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

The above "Device.OpenUri();" has now been deprecated so you'll have to use "Launcher.OpenAsync();" instead like so:

await Launcher.OpenAsync(new Uri(""))

Please see docs here:

Your Answer

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

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest