3.)文件下载
// 创建url
NSString *urlStr =@"http://www.easck.com/blog/950883/201701/950883-20170105104233581-62069155.png";
NSURL *Url = [NSURL URLWithString:urlStr];
// 创建请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:Url];
// 设置请求超时
[request setTimeoutInterval:30.0];
// 创建会话
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDownloadTask *downLoadTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (!error) {
NSLog(@"download sucess : %@", location);
NSData *data=[NSData dataWithContentsOfURL:location];
UIImage *image=[UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
// 刷新界面...
UIImageView *imageView =[[UIImageView alloc]init];
imageView.image=image;
[self.view addSubview:imageView];
[imageView mas_makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.view);
make.size.mas_equalTo(CGSizeMake(300, 300));
}];
});
} else {
NSLog(@"download error : %@", error.localizedDescription);
}
}];
//启动任务
[downLoadTask resume];
总结:
今天学习了iOS底层如何实现网络请求的,为了开发效率还得依靠优秀的第三方开源框架,以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持ASPKU。
注:相关教程知识阅读请移步到IOS开发频道。










