Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URLWithString: returns nil

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];
like image 796
gcamp Avatar asked Dec 30 '09 17:12

gcamp


4 Answers

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.

like image 152
gerry3 Avatar answered Nov 13 '22 07:11

gerry3


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

NSURL *_url = [NSURL fileURLWithPath:path];
like image 45
Islam.Ibrahim Avatar answered Nov 13 '22 07:11

Islam.Ibrahim


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.

like image 8
Yuji Avatar answered Nov 13 '22 07:11

Yuji


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

like image 2
Ben Gottlieb Avatar answered Nov 13 '22 08:11

Ben Gottlieb