我最初的问题的答案使用以下代码。
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:self.urlNameInput.text] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[NSURLConnection sendAsynchronousRequest:theRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *connection, NSData *data, NSError *error){...}];
我的原始代码不起作用,但如下所示。我的代码使用了 Apple Docs 中建议的一组委托方法(connection:didReceiveResponse:、connection:didReceiveData:、connection:didFailWithError: 和 connectionDidFinishLoading:)。
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlNameInput.text] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
receivedData = [NSMutableData data] ;
} else {
// Inform the user that the connection failed.
NSLog(@"Their is an error with that URL.");
};
委托方法是否与建议的代码兼容,如果是,我如何将它们集成到建议的代码中?