2013-07-13 70 views
0

创建NSURL我有一个UISearchBar我从中抽取代表从它的地址和建设JSON的URL地址解析器谷歌文本。自定义查询参数返回nil

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

如果我复制&从调试窗口的URL粘贴到它的工作原理就像一个魅力的浏览器,但是当我尝试将其转换为NSURL我得到nil

NSURL* theUrl = [NSURL URLWithString:url]; 

任何想法?

+0

您的网址似乎罚款,请尝试使用NSMutableRequest和不断变化的HTTP头,一不同的用户代理 –

回答

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];