94

it may be very easy, but I don't seems to find out why is URLWithString: returning nil here.

//localisationName is a arbitrary string here
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString* stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false&key=", webName];
NSURL* url = [NSURL URLWithString:stringURL];
gcamp
  • 14,622
  • 4
  • 54
  • 85
  • 1
    What is the value of `webName` before your call to `stringWithFormat:`? Then, what is the value of `stringURL` before your call to `URLWithString:`? Use `NSLog()` to print them out step by step, or, set breakpoints and inspect the values as they are set. – easeout Dec 30 '09 at 17:38

8 Answers8

201

You need to escape the non-ASCII characters in your hardcoded URL as well:

//localisationName is a arbitrary string here
NSString* webName = [localisationName stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSString* stringURL = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@,Montréal,Communauté-Urbaine-de-Montréal,Québec,Canadae&output=csv&oe=utf8&sensor=false", webName];
NSString* webStringURL = [stringURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL* url = [NSURL URLWithString:webStringURL];

You can probably remove the escaping of the localisationName since it will be handled by the escaping of the whole string.

gerry3
  • 21,420
  • 9
  • 66
  • 74
  • Trying to understand why URLWithString is giving nil here. Apple docs say "Must be a URL that conforms to RFC 2396. This method parses URLString according to RFCs 1738 and 1808.". Doing some reading... – russau Feb 02 '14 at 16:19
  • 1
    LOL, I copied a URL from an email and this happened! Thanks. – Echelon Jan 30 '15 at 17:37
  • 1
    breaks if the URL has a # in it. `stringByAddingPercentEscapesUsingEncoding` converts # to %2 – Ali Saeed Mar 02 '16 at 21:21
  • 3
    Above API is deprecated. You could use the following API `webStringURL = [stringURL stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];` – Rushabh Oct 24 '17 at 00:37
41

Use This Function if you deal with file saved on file manager.

NSURL *_url = [NSURL fileURLWithPath:path];
Islam.Ibrahim
  • 789
  • 6
  • 19
8

I guess you need to use -[NSString stringByAddingPercentEscapesUsingEncoding:]. See Apple doc.

Another comment is that, as an old timer, I find it a bit uneasy to put non-ASCII characters in a source file. That said, this Apple doc says, starting from 10.4, UTF-16 strings are OK inside @"...". Somehow GCC seems to correctly convert the source file in Latin-1 into UTF-16 in the binary, but I think it's safest to use 7-bit ASCII characters only inside the source code, and use NSLocalizedString.

Yuji
  • 34,103
  • 3
  • 70
  • 88
2

I think your accented characters are throwing things off; they won't be handled by -stringByAddingPercentEscapesUsingEncoding:.

Ben Gottlieb
  • 85,404
  • 22
  • 176
  • 172
2

NSURL URLWithString:@"" will return nil if the URL does not conform to RFC 2396 and they must be escaped

If you read through rfc2396 in the link you will get loads of details

A great site I found for checking where the offending character is, choose the path option for URL

http://www.websitedev.de/temp/rfc2396-check.html.gz

Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
1

UPDATE: Since stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding is deprecated now, you should use stringByAddingPercentEncodingWithAllowedCharacters

[stringURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

Here's the list of allowed characters set depending on your case.

+ (NSCharacterSet *)URLUserAllowedCharacterSet;
+ (NSCharacterSet *)URLPasswordAllowedCharacterSet;
+ (NSCharacterSet *)URLHostAllowedCharacterSet;
+ (NSCharacterSet *)URLPathAllowedCharacterSet;
+ (NSCharacterSet *)URLQueryAllowedCharacterSet;
+ (NSCharacterSet *)URLFragmentAllowedCharacterSet;

Reference: https://developer.apple.com/reference/foundation/nsstring/1411946-stringbyaddingpercentencodingwit

Fares Benhamouda
  • 589
  • 8
  • 21
0

The URLWithString: call will return a nil if the string passed to it is malformed. Since NSURL returns nil for malformed urls, NSLog your string and set breakpoints to see exactly what is being passed to your NSURL creation method. If your URLWithString works with a hard coded value, that's further proof that whatever you are passing is malformed.see

Community
  • 1
  • 1
Iggy
  • 8,463
  • 3
  • 31
  • 21
-1

You can use NSURL directly without NSString.

//.h file

@interface NewsBrowser : UIViewController {

    UIWebView *webView;
    NSURL *NewsUrl;

}

@property (nonatomic, retain) IBOutlet UIWebView *webView;

@property(nonatomic,assign)NSURL *NewsUrl;

@end

//.m file

[webView loadRequest:[NSURLRequest requestWithURL:NewsUrl]];

And I pass the URL from another view (using NewsUrl variable) to this view.

Try it.

The_Fox
  • 6,992
  • 2
  • 43
  • 69
fady
  • 31
  • 1
  • This answer didn't address the question at all, which was asking why URLWithString: was returning nil. (Further, you didn't even show a means of changing a string to a URL.) – ArtOfWarfare Apr 13 '13 at 00:27