2013-07-13 108 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];