【问题标题】:connectionDidFinishLoading: not called with the use of [NSURLConnection sendAsynchronousRequest: queue: completionHandler:connectionDidFinishLoading:未使用 [NSURLConnection sendAsynchronousRequest:queue:completionHandler:调用
【发布时间】:2017-02-27 15:52:13
【问题描述】:

[NSURLConnection sendAsynchronousRequest: 的答案获得良好的状态码时,我想使用 NSMutableURLRequest 发起请求。当我单独启动包含 NSMutableURLRequestexecuteForStatusOkMetod 时,它运行良好。

但如果我使用 [NSURLConnection sendAsynchronousRequest: 启动 executeForStatusOkMetod,则永远不会调用 connectionDidFinishLoading: 函数。

这里是主要代码:

 NSOperationQueue *myQueue = [[NSOperationQueue alloc] init];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
    request.timeoutInterval = 10;
    [NSURLConnection sendAsynchronousRequest:request queue:myQueue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
        NSLog(@"response status code: %ld, error status : %@", (long)[httpResponse statusCode], error.description);

        if ((long)[httpResponse statusCode] >= 200 && (long)[httpResponse statusCode]< 400)
            {
               // do stuff
                [[DataManagement sharedManager] executeForStatusOkMetod];
            }
    }];

这是调用的函数:

(void) executeForStatusOkMetod
{
 NSMutableURLRequest* request = [[NSMutableURLRequest alloc] init];
        NSString *url_string = [bytes stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [request setURL:[NSURL URLWithString:[set_send_order stringByAppendingString: url_string]]];
        [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
        [request setTimeoutInterval:timeOut];
        [NSURLConnection connectionWithRequest:request delegate:self]; 
}

为什么 connectionDidFinishLoading: 没有通过调用 [NSURLConnection sendAsynchronousRequest: 方法中的 NSMutableURLRequest 来调用?

【问题讨论】:

  • connectionWithRequest:delegate 返回一个 NSURLConnection 对象。我想您可能需要致电startNSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self]; [connection start];?此外,由于您使用完成块,它不会调用委托。这是一个或另一个。您可以强制调用块中的委托。
  • 没有人应该再使用NSURLConnection,它已经被弃用了。从现在开始应该使用NSURLSession。或者说真的,AFNetworking 或 Alamofire。
  • @Larme 所以如果我不能同时使用,我怎么能告诉他在调用 WS 之前先检查 Wi-Fi 中是否有本地连接?
  • 如果要检查您的连接,您可能对 Reachability 感兴趣。

标签: ios objective-c xcode nsurlconnection sendasynchronousrequest


【解决方案1】:

在第一个示例中,未调用委托方法,因为 API 不包含设置委托的参数。

在带有完成处理程序的 API 中,连接在完成处理程序执行时完成。

【讨论】:

  • 你认为我不能在 sendAsynchronousRequest 的响应中使用 NSMutableURLRequest 吗?
  • 可以,请求被捕获到block中。
  • 嗯好的,如何在 API 中设置委托?
  • 你不能。要么将sendAsynchronousRequest 与完成处理程序一起使用,要么将connectionWithRequest 与委托一起使用。实际上,您只需要委托方法来进行更精细的控制,例如处理凭据和重定向。
  • mmm 你知道我是否可以简单地测试一下是否有 WIFI 本地连接?例如,如果 url 192.168.0.100 是可访问的。我尝试了可达性但没有成功。它告诉我它是连接的,但它不是真的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
  • 2011-10-20
  • 1970-01-01
  • 1970-01-01
  • 2012-11-01
  • 1970-01-01
相关资源
最近更新 更多