iOS基础知识—————基础不牢,地动山摇
Objective-C 之 网络请求
URL
1、全称:Uniform Resource Locator 统一资源定位符
2、URL对象建立:
//创建URL从网络服务器
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
//创建URL从本地文件
NSURL *url = [NSURL fileURLWithPath:@"/Users/apple/desktop/text.txt"];
3、URLRequest 对象建立:
//默认的Request
NSURLRequest *req = [NSURLRequest requestWithURL:url];
//设定缓存策略,及网络请求超时时间
NSURLRequest *req1 = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
4、URLConnection的异步请求
[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (connectionError) {
NSLog(@"error:%@",connectionError);
}else{
NSLog(@"response:%@",response);
NSLog(@"datalength:%lu",data.length);
}
}];
5、NSConnectionDataDelegate代理方法
NSURLConnection *connect = [NSURLConnection connectionWithRequest:req delegate:self];
//四个代理方法
//网络开始响应后,代理自动调用
- (void)connection:(NSURLConnection *)connection didReceiv

本文深入探讨iOS基础知识中的Objective-C网络请求,包括URL的使用,URLRequest对象建立,URLConnection的异步请求及NSConnectionDataDelegate代理方法。进一步讲解了使用Hpple解析HTML网页和URLSession的网络操作,特别提到了Session的download data和data delegate,以及对比了AFNetworking的使用。
最低0.47元/天 解锁文章
3486

被折叠的 条评论
为什么被折叠?



