Valid URL string as NSUrl becomes null

another issue where I seem to have found a solution for ObjC but not MonoTouch.

I want to get NSUrl from a url (as a string).

A string can contain spaces and backslashes.

Why does NSUrl return null for such a string, even if these are valid URLs in the browser?

For example: NSUrl foo = NSUrl.FromString (@ " http://google.com/search ? \ Query");

foo == null

Any suggestions?

+6
source share
4 answers

[NSURL URLWithString: [googlSearchString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];

Link to
Stack Overflow ...

+13
source share

You probably need to process the string first with

stringByAddingPercentEscapesUsingEncoding: 

... so that it can handle a valid URL.

+1
source share

URLWithString: Creates and returns an NSURL object initialized with the provided string.

  • (id) URLWithString: (NSString *) URLString parameters URLString String to initialize the NSURL object. There must be a URL that complies with RFC 2396. This method parses the URLString in accordance with RFC 1738 and 1808. (To create NSURL objects for file system paths, use fileURLWithPath: isDirectory: instead.) Return Value The NSURL object is initialized with a URLString. If the string was invalid, returns nil.
0
source share

NSString * urlString = @"http://example/newcase/path/fileNames"; urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding]; NSURL * url = [NSURL URLWithString:urlString]; NSLog(@"URL: %@", url);

0
source share

All Articles