iOS网络: NSURLConnection进行同步下载
例子:
- (void)sendSynNetwork{ NSLog(@"Wo are here..."); NSString *urlString = @"http://www.baudu.com"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f]; //同步没有block //这样解决比较方便 NSURLResponse *response = nil; NSError *error = nil; NSLog(@"开始同步请求"); NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; if ([data length] > 0 && error == nil){ NSLog(@"%lu bytes of data was returned.", (unsigned long)[data length]); } else if ([data length] == 0 && error == nil){ NSLog(@"No data was returned."); } else if (error != nil){ NSLog(@"Error happened = %@", error); } NSLog(@"We are done."); }
Wo are here...
开始同步请求
8875 bytes of data was returned.
We are done.
//简单的同步请求 - (void)sendSynNetwork{ NSLog(@"Wo are here..."); NSString *urlString = @"http://www.baudu.com"; NSURL *url = [NSURL URLWithString:urlString]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f]; dispatch_queue_t dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(dispatchQueue, ^{ //同步没有block //这样解决比较方便 NSURLResponse *response = nil; NSError *error = nil; NSLog(@"开始同步请求"); NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:&error]; if ([data length] > 0 && error == nil){ NSLog(@"%lu bytes of data was returned.", (unsigned long)[data length]); } else if ([data length] == 0 && error == nil){ NSLog(@"No data was returned."); } else if (error != nil){ NSLog(@"Error happened = %@", error); } }); NSLog(@"We are done."); }
Wo are here...
We are done.
开始同步请求
9138 bytes of data was returned.
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。