NSURL *httpsURL = [NSURL URLWithString:@"https://example.com"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:httpsURL cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:5.0f];
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
[self.connection start];
while(something) {
// some code
}
在上面的代码中,代表(例如:willSendRequestForAuthenticationChallenge
)不执行,直到while
循环就完成了。这种行为的原因是什么以及如何解决这个问题?我希望代表在后台执行。我怎样才能实现它?NSURLConnection的委托方法不执行
在此先感谢。
请勿使用NSURLConnection。它已被弃用。 – matt
不相关,但不要调用'start',因为'connectionWithRequest:delegate:'自动启动连接。你唯一一次调用'start'就是当你用'startImmediately'设置为'false'来启动连接的时候。但它没有实际意义,因为你应该使用'NSURLSession'。 – Rob