开发者

Why does my NSURL object is equal to nil? My path is correct

开发者 https://www.devze.com 2023-03-16 01:55 出处:网络
NSString *urlString = [NSString stringWithFormat:@\"http://maps.google.com/maps/geo?q=%lf,%lf&output=csv&sensor=false&key=swizzlec hops\", coordinate.latitude,coordinate.longitude];
NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%lf,%lf&output=csv&sensor=false&key=swizzlec hops", coordinate.latitude,coordinate.longitude];
NSLog(@"urlString: %@", urlString); 
NSURL *urlFromURLString = [NSURL URLWithString:urlString];

My log is : http://maps.google.com/maps/geo?q=53.872874,27.527790&output=csv&sensor=false&key=swizzlec hops

I can copy this url and paste to the browser and its ok, but urlFromURLString = nil. 开发者_运维技巧But, why?


I believe you need to use

stringByAddingPercentEscapesUsingEncoding:

on urlString before passing it to NSURL.

NSString *urlString = [[NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%lf,%lf&output=csv&sensor=false&key=swizzlec hops", coordinate.latitude,coordinate.longitude] stringByAddingPercentEscapesUsingEncoding::NSUTF8StringEncoding];


Try

 NSString *urlString = [[NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%lf,%lf&output=csv&sensor=false&key=swizzlec hops", coordinate.latitude,coordinate.longitude] stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

I don't think you can have spaces in your URL


This is a better approach:

NSString* escapedUrl = [originalUrl   stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *urlFromURLString = [NSURL URLWithString:escapedUrl];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号