2016-05-04 6 views
1

J'utilise ci-dessous le code, mais il ne fonctionne pas pour moi et toujours obtenir une réponse nulle mais quand ce ravageur URL dans le navigateur il téléchargé un fichier f.txt qui contiennent en dessous du texteYoutube API recherche complète-automatique ne fonctionne pas

window.google.ac.h (["Y", [["jeune voyou", 0], ["youtube", 0], ["jeunesse troye sivan", 0], ["jeune dauph" , 0], ["yo gotti", 0], ["yg", 0], ["yoga", 0], ["vous devrait être ici cole swindell", 0], ["yandere simulator", 0 ], [ "jeune gangster meilleur ami", 0]], { "k": 1, "l": "1", "q": "Fu65vJmwPDpRvrCvJ_hO3MqI15U"}])

@property(strong, nonatomic) NSMutableArray *ParsingArray // Put that in .h file or after @interface in your .m file 

    -(void)autocompleteSegesstions : (NSString *)searchWish{ 
//searchWish is the text from your search bar (self.searchBar.text) 


NSString *jsonString = [NSString stringWithFormat:@"http://suggestqueries.google.com/complete/search?client=youtube&ds=yt&alt=json&q=%@", searchWish]; 
    NSString *URLString = [jsonString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; // Encoding to identify where, for example, there are spaces in your query. 


NSLog(@"%@", URLString); 

    NSData *allVideosData = [[NSData alloc]initWithContentsOfURL:[[NSURL alloc]initWithString:URLString]]; 

NSString *str = [[NSString alloc]initWithData:allVideosData encoding:NSUTF8StringEncoding]; 
NSLog(@"%@", str); //Now you have NSString contain JSON. 
NSString *json = nil; 
NSScanner *scanner = [NSScanner scannerWithString:str]; 
[scanner scanUpToString:@"[[" intoString:NULL]; // Scan to where the JSON begins 
[scanner scanUpToString:@"]]" intoString:&json]; 
//The idea is to identify where the "real" JSON begins and ends. 
json = [NSString stringWithFormat:@"%@%@", json, @"]]"]; 
NSLog(@"json = %@", json); 


NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] //Push all the JSON autocomplete detail in to jsonObject array. 
                       options:0 error:NULL]; 
self.ParsingArray = [[NSMutableArray alloc]init]; //array that contains the objects. 
for (int i=0; i != [jsonObject count]; i++) { 
    for (int j=0; j != 1; j++) { 
     NSLog(@"%@", [[jsonObject objectAtIndex:i] objectAtIndex:j]); 
     [self.ParsingArray addObject:[[jsonObject objectAtIndex:i] objectAtIndex:j]]; 
     //Parse the JSON here... 

    } 

}} 

Répondre

1

pour résoudre ce Il y a deux choses à savoir ici:

Dans iOS 9, par défaut, http: // n'est pas pris en charge. Vous devez communiquer en toute sécurité (avec https: //). Vous pouvez désactiver cette fonctionnalité dans votre Info.plist si nécessaire. Les URL NSFileManager doivent être des chemins d'accès aux fichiers sur le disque, c'est-à-dire qu'ils doivent être des URL de fichiers. Le vôtre ne l'est pas; c'est une URL http: //. Si votre objectif est de télécharger un fichier et de le copier quelque part, utilisez la tâche de téléchargement de NSURLSession.