這就是我做的:的 代替
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]
我提出基於同樣的方法例如,在包含類,因爲我們將需要一個代表。不要讓它成爲單例,所以每個連接都有其獨立變量,因爲如果我們不這樣做,並且在另一個連接完成之前碰巧調用了兩個連接,那麼接收到的數據和循環的處理將不可挽回地交織在一起。
[[ClassNameHere new] sendSynchronousRequest:request returningResponse:&response error:&error]
這樣我可以創建一個NSURL連接,並處理它(以同步的方式,我們將看到如何),所以我沒有改變任何以前編寫的代碼的。
- (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse *__strong*)response error:(NSError *__strong*)error
{
_finishedLoading=NO;
_receivedData=[NSMutableData new];
_error=error;
_response=response;
NSURLConnection*con=[NSURLConnection connectionWithRequest:request delegate:self];
[con start];
CFRunLoopRun();
return _receivedData;
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
//handle the challenge
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
*_response=response;
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
*_error=error;
CFRunLoopStop(CFRunLoopGetCurrent());
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
CFRunLoopStop(CFRunLoopGetCurrent());
}
訣竅是在CFRunLoopRun()和CFRunLoopStop(CFRunLoopGetCurrent()) 我希望它可以幫助別人在未來的危機別的。
這個傢伙需要一個實例相關的函數調用,以便提供委託來響應https挑戰 – LolaRun 2013-07-01 09:48:28
謝謝:)它非常簡單。 – 2013-07-03 11:03:48