2013-10-29 100 views
0

我正在嘗試在YouTube上進行搜索。此代碼適用於英文字符。但是當我嘗試輸入非英文字母時,如ö,ü,則返回null。URL上的字符編碼問題,非英文字符-iOS

NSString *kStrJsonURL = [NSString stringWithFormat:@"http://suggestqueries.google.com/complete/search?hl=en&ds=yt&client=youtube&hjson=t&cp=1&q=%@&key=API_KEY&format=5&alt=jsonc&callback=?", self.searchField.text]; 
    NSURL *url = [NSURL URLWithString:kStrJsonURL]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id getJSON) { 
     _JSON = getJSON; 
     NSLog(@"%@", _JSON); 

    } failure:nil]; 
    [operation start]; 

我試圖編碼URL這樣可是不行的..

[self.searchField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] 

回答

0

您的代碼應該是這樣的:

//Added this line 
[self.searchField.text stringByReplacingOccurrencesOfString:@" " withString:@"+"]; 
// 
    NSString *kStrJsonURL = [NSString stringWithFormat:@"http://suggestqueries.google.com/complete/search?hl=en&ds=yt&client=youtube&hjson=t&cp=1&q=%@&key=API_KEY&format=5&alt=jsonc&callback=?", self.searchField.text]; 
     NSURL *url = [NSURL URLWithString:[kStrJsonURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
     NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
     AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id getJSON) { 
      _JSON = getJSON; 
      NSLog(@"%@", _JSON); 

    } failure:nil]; 
    [operation start]; 
+0

我只是嘗試這樣做,並檢查了故障,我收到此錯誤:NSDebugDescription =「無法將數據轉換爲字符1周圍的字符串。」; – onivi

+0

編輯我的答案。看看是否有幫助。 –

+0

不幸的是..還是一樣的錯誤.. – onivi