Bug 29247 - iOS Device.OpenUri breaks with encoded params
Summary: iOS Device.OpenUri breaks with encoded params
Alias: None
Product: Forms
Classification: Xamarin
Component: iOS ()
Version: 1.4.1
Hardware: PC Windows
: Normal normal
Target Milestone: ---
Assignee: Bugzilla
: 23878 ()
Depends on:
Reported: 2015-04-20 13:27 UTC by Nick Spiers
Modified: 2017-08-29 22:56 UTC (History)
8 users (show)

Tags: ac device
Is this bug a regression?: ---
Last known good build:

Device Open Uri Exception Recreation (50 bytes, text/plain)
2015-04-20 13:39 UTC, Nick Spiers

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Nick Spiers 2015-04-20 13:27:29 UTC
When using Device.OpenUri with any uri with percent encoded params I get the following exception:

System.Exception: Could not initialize an instance of the type 'Foundation.NSUrl': the native 'initWithString:' method returned nil.

I'm using Xamarin.Forms

I'm attaching a sample project that recreates the issue.

Thanks, Nick.
Comment 1 Nick Spiers 2015-04-20 13:39:41 UTC
Created attachment 10825 [details]
Device Open Uri Exception Recreation
Comment 2 Rui Marinho 2015-06-15 14:35:43 UTC
Should be fixed in 1.4.3 final
Comment 3 Parmendra Kumar 2015-06-16 09:31:14 UTC
I have checked this issue with Xamarin.Forms and its working
fine at my end.

Hence closing this issue.

Environment info:

Microsoft Visual Studio Professional 2013
Version 12.0.30723.00 Update 3
Microsoft .NET Framework
Version 4.5.51641

Xamarin   3.11.590.0 
Comment 4 Stephane Delcroix 2015-07-22 09:40:47 UTC
*** Bug 23878 has been marked as a duplicate of this bug. ***
Comment 5 alex 2017-01-30 20:01:49 UTC
Ok, I have just caught this bug with latest XS beta. Also reproduceable on stable branch.
Workaround is as suggested here: https://forums.xamarin.com/discussion/comment/120461/#Comment_120461

=== Xamarin Studio Enterprise ===

Version 6.2 (build 1778)
Installation UUID: 3b2af2ab-cb5d-416a-bb51-806e01d7227c
	Mono 4.8.0 (mono-4.8.0-branch/6f90ed1) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 408000472

=== NuGet ===


=== Xamarin.Profiler ===

Version: 1.1.6
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Xamarin Inspector ===

Hash: 1f3067d
Branch: master
Build date: Tue, 15 Nov 2016 21:13:59 GMT

=== Xamarin.Android ===

Version: (Visual Studio Enterprise)
Android SDK: /Users/username/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		2.3   (API level 10)
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)
		7.0   (API level 24)
		7.1   (API level 25)

SDK Tools Version: 25.2.3
SDK Platform Tools Version: 25
SDK Build Tools Version: 23.0.3

Java SDK: /usr
java version "1.8.0_60"
Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

Android Designer EPL code available here:

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Apple Developer Tools ===

Xcode 8.2.1 (11766.1)
Build 8C1002

=== Xamarin.iOS ===

Version: (Visual Studio Enterprise)
Hash: 4e31137
Branch: cycle9
Build date: 2017-01-20 13:08:53-0500

=== Xamarin.Mac ===

Version: (Visual Studio Enterprise)

=== Build Information ===

Release ID: 602001778
Git revision: 615fe0299995fbf4023cf95980615013b1cf8d74
Build date: 2017-01-20 08:31:58-05
Xamarin addins: f53fb5ce32a48c49c329af8d1b75a3ea1b2c2ff1
Build lane: monodevelop-lion-cycle9

=== Operating System ===

Mac OS X 10.12.1
Darwin MacBook-Pro-2.local 16.1.0 Darwin Kernel Version 16.1.0
    Thu Oct 13 21:26:57 PDT 2016
    root:xnu-3789.21.3~60/RELEASE_X86_64 x86_64

=== Enabled user installed addins ===

Addin Maker 1.3.2
NuGet Package Management Extensions 0.12
StyleCop Support
SortAndRemoveOnSave 1.0
Comment 7 Rui Marinho 2017-01-31 12:56:44 UTC
Thanks, new pr up.

Comment 8 Philipp Sumi 2017-08-09 10:23:26 UTC
Just stumbled over this. I'm very late to that party, but the workaround in that PR ignores any guery parameters (since you use LocalPart) and would thus result in an incomplete URI.
Comment 9 Philip.Apptelic 2017-08-21 14:47:20 UTC
I agreed with Philipp - this line:


Contains this code:

... ?? new NSUrl(uri.Scheme, uri.Host, uri.LocalPath);

Which indeed does not include the query string.

Do we need to open a new bug for this?

Comment 10 Philip.Apptelic 2017-08-21 14:49:37 UTC
Still present in 3.0 branch:

Comment 11 Philip.Apptelic 2017-08-21 16:16:45 UTC
OK, it appears the NSUrl.FromString() method doesn't like being given a string with spaces in it, and the System.Uri.ToString() method is URL-decoding the given string (except @, / and # characters, but that's a different story!), resulting in the NSUrl.FromString failing and returning a null, which then falls back to the null coalescing operator's right-hand expression which re-forms the URL string from the Uri's components *without* the query string.

So, the offending line linked to above can be changed to this to fix the problem:

// convert from original string, but if that fails concatenate component parts
var url = NSUrl.FromString (uri.OriginalString) ?? new NSUrl ($"{uri.Scheme}://{uri.Host}{uri.LocalPath}{uri.Query}");

This a) uses Uri.OriginalString to ensure the NSUrl.FromString() works in more cases, and b) if that fails, creates a new NSUrl with all component parts of the original Uri, including the query string.

We have now implemented this in our own platform helper code and it works for all URLs we've tried, so we're now using it instead of Device.OpenUri() in all our projects.

Comment 12 Jimmy [MSFT] 2017-08-29 22:54:45 UTC
Hey all, a new report has been opened for this issue on Forms 2.4.0. Please follow the newer report for updates. Thanks!