我有一个UISearchBar,我从中提取代表地址的文本,并从中构建Google Geocoder的JSON URL。
NSString* address = searchbar.text;
NSString* url = [NSString stringWithFormat:@"http://maps.google.com/maps/api/geocode/json?address=%@&sensor=false", address];
如果我复制&将调试窗口中的网址粘贴到浏览器,它就像魅力一样,但当我尝试将其转换为NSURL时,我得到nil
NSURL* theUrl = [NSURL URLWithString:url];
有什么想法吗?
答案 0 :(得分: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];