Launcher.OpenAsync(uri); method call crashes / throws an exception in case any space in the query url.
The issue is only present in iOS. Android everything working as expected.
My button click event
async void Handle_Clicked(object sender, System.EventArgs e)
{
string url = string.Empty;
switch (Device.RuntimePlatform)
{
case Device.iOS:
url = $"https://maps.apple.com/maps?q=Ole Vigs Gate 8B";
break;
case Device.Android:
url = $"https://maps.google.com/maps?q=Ole Vigs Gate 8B";
break;
}
await LaunchApplicationAsync(url);
}
Launcher call
public async Task LaunchApplicationAsync(string uri)
{
try
{
await Launcher.OpenAsync(uri);
}
catch (Exception ex)
{
//Ignore
}
}
Stack Trace.
{System.Exception: Could not initialize an instance of the type 'Foundation.NSUrl': the native 'initWithString:' method returned nil.
It is possible to ignore this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to false.
at Foundation.NSObject.InitializeHandle (System.IntPtr handle, System.String initSelector) [0x000a8] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/Foundation/NSObject2.cs:504
at Foundation.NSUrl..ctor (System.String urlString) [0x00023] in /Library/Frameworks/Xamarin.iOS.framework/Versions/12.8.0.2/src/Xamarin.iOS/Foundation/NSUrl.g.cs:139
at Xamarin.Essentials.Launcher.PlatformOpenAsync (System.Uri uri) [0x0000b] in
at Xamarin.Essentials.Launcher.OpenAsync (System.String uri) [0x00019] in
at LauncherIssue.MainPage.LaunchApplicationAsync (System.String uri) [0x00018] in /Users/rakeshrnair/Projects/LauncherIssue/LauncherIssue/MainPage.xaml.cs:30 }
I can reproduce the issue in all iOS devices.
@jamesmontemagno
If there is any special character present in the url it throws the exception. I am also facing the same issue. Even with the url encoded string is not working. I assume the issue is with uri formatting.
I confirm, Android it is working fine.
Need help.
I will try to reproduce on my end... odd.
@soooraj do you have any other other urls that I can test?
Sadly no ... I tried opening the map url only.
If it is only opening Map, what about trying "Maps" in Xamarin Essentials. ?? But I am not sure if you can give address directly like in your query string. @jamesmontemagno can clarify this.
I think,
For Map we need to provide more details like
var placemark = new Placemark
{
CountryName = "",
AdminArea = "",
Thoroughfare = "",
Locality = ""
};
In my case, I don't have much information. Only street address I have. Not sure about @rkshnair use case.
But I believe this is a general use case. In our native implementation (current iphone app ) also we are opening the URL in same way only.
@jamesmontemagno Sorry for the delay. It will fail all the urls with a query string / special character / space etc.
https://twitter.com/search?q=xamarin Forms
You can try this url of twitter.
Is there a reason that you aren't using the Browser API to open it?
I have a fix coming in, but if you use the Browser API this will work just fine for you.
Most helpful comment
I have a fix coming in, but if you use the Browser API this will work just fine for you.