如果方法使用NSURLConnection如何返回输出与块的asynchronous调用

我正在向服务器发送请求以使用NSURLConnection块方法获得响应。 我不能使用返回YES或在块中返回NO。 我怎样才能解决这个问题?

-(BOOL)checkForValidUrl:(NSString *)url { __block int httpStatus=0; NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]]; [request setValue:@"application/json" forHTTPHeaderField:@"content-type"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error) { httpStatus = [((NSHTTPURLResponse *)response) statusCode]; NSLog(@"httpStatus inside block:%d",httpStatus); } ]; return NO; } 

不要返回任何东西,而是传递一个完成块,稍后通知调用者检查结果。 您将不得不重新调用代码,不需要立即结果。

例如,假设200响应意味着URL是有效的:

 -(void)checkForValidUrl:(NSString *)url completion:(void (^)(BOOL validURL))completion { __block int httpStatus=0; NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]]; [request setValue:@"application/json" forHTTPHeaderField:@"content-type"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error) { httpStatus = [((NSHTTPURLResponse *)response) statusCode]; NSLog(@"httpStatus inside block:%d",httpStatus); completion(200 == httpStatus); } ]; } 

不可能。 当应用程序调用return语句时,连接结果是未知的。 你应该使用类似的东西

 [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error) { httpStatus = [((NSHTTPURLResponse *)response) statusCode]; NSLog(@"httpStatus inside block:%d",httpStatus); [_your_delegate_object connectionFinishedWithStatus:httpStatus data:responseData]; } ]; 

应该是这样的

 -(void)checkForValidUrl:(NSString *)url completion:(void(^)(BOOL success))completion { __block int httpStatus=0; NSMutableURLRequest *request=[[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]]; [request setValue:@"application/json" forHTTPHeaderField:@"content-type"]; [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *responseData, NSError *error) { httpStatus = [((NSHTTPURLResponse *)response) statusCode]; NSLog(@"httpStatus inside block:%d",httpStatus); completion(httpStatus == 200) //if Yes or No depends on the status } ]; } 

你的DataFetchController.h文件

 - (void)beginLeadTaskWithCallbackBlock:(void (^)(NSArray *))callbackBlock; 

你的DataFetchController.m文件

 -(void)beginLeadTaskWithCallbackBlock:(void (^)(NSArray *))callbackBlock{ // add your post code here [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; if ([data length] >0 && error == nil && [httpResponse statusCode] == 200) { NSArray *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; NSLog(@"data %@",dictionary); callbackBlock(dictionary); } }]; } 

块调用你的视图控制器

使用不足以避免内存泄漏

  DataFetchController *getFetch=[[DataFetchController alloc]init]; DataFetchController * __weak weakDataFetch=getFetch; [weakDataFetch beginLeadTaskWithCallbackBlock:^(NSArray *mm){ dispatch_async(dispatch_get_main_queue(), ^{ //update main UI }); }];