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...
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 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>
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: