我正在运行 Apple 的以下示例代码 -
NSString *requestURL = [[NSString alloc] initWithString:@"http://google.com/"];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:requestURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(theConnection) {
NSLog(@"The connection has started...");
} else {
NSLog(@"Connection has failed...");
}
但下面的委托方法没有被调用。有人可以帮我吗?
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
如果您在后台线程上执行此操作,则该线程可能会在调用委托之前退出。查看调用启动后台线程的方法 CFRunLoopRun() 后启动传输的方法。然后在你的回调中connectionDidFinishLoading:
and connection:didFailWithError:
确保您还运行:
CFRunLoopStop(CFRunLoopGetCurrent());
在这些方法结束时,否则你将永远不会返回。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)