0

I have a UISearchBar from which I'm extracting the text that represents an address and building from it a JSON URL for Google Geocoder.

NSString* address = searchbar.text;
NSString* url = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?address=%@&sensor=false", address];

If I copy & paste the url from debug window to the browser it works like a charm but when I try to convert it to NSURL i get nil

NSURL* theUrl = [NSURL URLWithString:url];

Any ideas?

4

1 回答 1

0

我在连接 url 之前将编码添加到地址并解决了问题,所以现在代码如下所示:

NSString* address = searchbar.text;
NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, CFBridgingRetain(address), NULL, (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8));

NSString* url = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?address=%@&sensor=false", encodedString];
于 2013-07-13T20:22:53.733 回答