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