Bug 29510 - NSUrl returns null when there are special characters in the url string
Summary: NSUrl returns null when there are special characters in the url string
Alias: None
Product: iOS
Classification: Xamarin
Component: General ()
Version: XI 8.10
Hardware: PC Mac OS
: High normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2015-04-29 02:53 UTC by Prashant Cholachagudda
Modified: 2015-04-29 21:43 UTC (History)
3 users (show)

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

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 Prashant Cholachagudda 2015-04-29 02:53:32 UTC
NSUrl returns null when there are special characters in the url string

	string UrlAddress = Uri.EscapeUriString("Server 1/Custom View/Analog Schedule!@#$%^&&%$#@");
	NSUrl testUrl = NSUrl.FromString(UrlAddress); //Returns null
Comment 4 Sebastien Pouliot 2015-04-29 21:43:28 UTC
That's normal. Apple NSUrl is much stricter than .NET Uri in what it accept. If the URL is not valid (without alteration, including spaces) then NSUrl will return null.

Mixing .NET API and Apple API is not always the best solution (they can duplicate work or leave in gaps between themselves).

Try using the extension method (on NSString) CreateStringByAddingPercentEscapes to prepare the string you'll give to NSUrl. That conversion will match what NSUrl wants. E.g.

	string bad = "Server 1/Custom View/Analog Schedule!@#$%^&&%$#@";

	string bad_url = Uri.EscapeUriString (bad);
	Assert.Null (NSUrl.FromString (bad_url), "bad"); 

	string converted = ((NSString) bad).CreateStringByAddingPercentEscapes (NSStringEncoding.UTF8);
	using (var url = NSUrl.FromString (converted)) {
		Console.WriteLine (url);