Kodlama: Biz will beri yerine
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]
I içeren sınıfına, esaslı aynı yöntem örneği yapılmış bir delege ihtiyacım var. Ve bunu tekil yapmayın, bu yüzden her bağlantının bağımsız değişkenleri vardır, çünkü eğer yapmazsak, ve diğeri bitmeden önce iki bağlantı çağrılırsa, o zaman alınan veri ve döngülerin ele geçirilmesi geri dönülemez şekilde iç içe geçecektir. .
[[ClassNameHere new] sendSynchronousRequest:request returningResponse:&response error:&error]
Bu şekilde ben bir NSUrl bağlantısı oluşturmak ve bunu işleyebilir (zaman uyumlu bir şekilde, nasıl göreceksiniz) bu yüzden daha önce yazılı kodun herhangi bir değişiklik gerekmez.
- (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());
}
hile CFRunLoopRun() ve CFRunLoopStop oldu (CFRunLoopGetCurrent()) Ben futur başkası yardımcı olur.
Adam, temsilci https sorununa yanıt vermek için bir örneğe bağımlı işlev çağrısı gerekiyor – LolaRun
Teşekkür ederim :) Bu çok basit. –