如何使用NSURLSession从块获取数据?

我有这个块的问题。 我试图获取NSURLSession块内的数据。

这是我的代码

 -(NSDictionary *) RetrieveData{ NSURLSession * session = [NSURLSession sharedSession]; NSURL * url = [NSURL URLWithString: self.getURL]; dataList =[[NSDictionary alloc] init]; NSURLSessionDataTask * dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { self.json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; }]; return self.dataList; [dataTask resume]; } 

是否有可能获得NSURLSession块内的数据?

 -(void)getJsonResponse:(NSString *)urlStr success:(void (^)(NSDictionary *responseDict))success failure:(void(^)(NSError* error))failure { NSURLSession *session = [NSURLSession sharedSession]; NSURL *url = [NSURL URLWithString:urlStr]; // Asynchronously API is hit here NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSLog(@"%@",data); if (error) failure(error); else { NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSLog(@"%@",json); success(json); } }]; [dataTask resume]; // Executed First } 

调用这个:

 [self getJsonResponse:@"Enter your url here" success:^(NSDictionary *responseDict) { NSLog(@"%@",responseDict); } failure:^(NSError *error) { // error handling here ... }]; 

你应该使用完成块,例如:

 - (void)retrieveData:(void (^)(NSDictionary * dictionary))completionHandler { NSURLSession *session = [NSURLSession sharedSession]; NSURL *url = [NSURL URLWithString: self.getURL]; NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; if (completionHandler) { completionHandler(dictionary); } }]; [dataTask resume]; } 

那么调用它的方法将会这样做:

 [self retrieveData:^(NSDictionary *dictionary) { // you can use the dictionary here // if you want to update UI or model, dispatch this to the main queue: dispatch_async(dispatch_get_main_queue(), ^{ // do your UI stuff here }); }]; // but dont try to use the dictionary here, because you will likely // hit this line before the above block fires off, and thus the // dictionary hasn't been returned yet! 

您正在调用采用完成块模式的asynchronous方法,因此您还应该在自己的代码中使用完成块模式。

你将不得不围绕这个方法。 最好你从RetrieveData(你违反cocoa命名约定在这里)重新命名你的方法startRetrievingData。 你不能写一个实际检索数据的方法,因为在最糟糕的情况下,这可能需要几分钟,你的用户会恨你。

调用它startRetrievingData,使其返回void,并传入两个块,当将来调用数据时将调用这个数据块,以及在发生错误时将会调用的一个块,并且您无法获取数据。

你不能返回数据。 不要问“我如何返回数据”,你不能。 你给代码一个块,当数据可用时被调用,并且该块负责处理数据,无论你想用它做什么。

 NSURLSession *session = [NSURLSession sharedSession]; NSURL *url = [NSURL URLWithString:@"http://www.code-brew.com/projects/Instamigos/api/login.php?instagram_id=572275360&access_token=572275360.4c70214.57e0ecb1113948c2b962646416cc0a18&name=dpak_29&uuid=ios_1"]; // Asynchronously API is hit here NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { NSLog(@"%@",data); // Executed when the response comes from server // Handle Response here NSDictionary * json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; NSLog(@"%@",json); }]; [dataTask resume]; // Executed First