0

I am trying to do search on youtube. This code works fine with english chars. But when I try to type non-english letters like ö, ü, ş returns null.

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

I've tried to encode url like this but doesn't work..

[self.searchField.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
onivi
  • 1,348
  • 3
  • 17
  • 25

2 Answers2

0

Hahve you tried URLEncoding kStrJsonURL? See the accepted answer for this post.

How do I URL encode a string

Community
  • 1
  • 1
Zoltan Varadi
  • 2,468
  • 2
  • 34
  • 51
0

Your code should like this:

//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];
Nikos M.
  • 13,685
  • 4
  • 47
  • 61
  • I just tried this and checked the failure and I am getting this error: NSDebugDescription = "Unable to convert data to string around character 1."; – onivi Oct 29 '13 at 16:06
  • Give this answer a try. http://stackoverflow.com/questions/14485868/nsjsonserialization-unable-to-convert-data-to-string – Nikos M. Oct 29 '13 at 16:16