【iOS基础】NSURLConnection
一、大文件下载
1.方案:利用NSURLConnection和它的代理方法
1> 发送一个请求
// 1.URL
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/resources/videos.zip"];
// 2.请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3.下载(创建完conn对象后,会自动发起一个异步请求)
[NSURLConnection connectionWithRequest:request delegate:self];
2> 在代理方法中处理服务器返回的数据
/**
在接收到服务器的响应时:
1.创建一个空的文件
2.用一个句柄对象关联这个空的文件,目的是:方便后面用句柄对象往文件后面写数据
*/
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// 文件路径
NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
NSString *filepath = [caches stringByAppendingPathComponent:@"videos.zip"]; // 创建一个空的文件 到 沙盒中
NSFileManager *mgr = [NSFileManager defaultManager];
[mgr createFileAtPath:filepath contents:nil attributes:nil]; // 创建一个用来写数据的文件句柄
self.writeHandle = [NSFileHandle fileHandleForWritingAtPath:filepath];
} /**
在接收到服务器返回的文件数据时,利用句柄对象往文件的最后面追加数据
*/
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
// 移动到文件的最后面
[self.writeHandle seekToEndOfFile]; // 将数据写入沙盒
[self.writeHandle writeData:data];
} /**
在所有数据接收完毕时,关闭句柄对象
*/
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
// 关闭文件
[self.writeHandle closeFile];
self.writeHandle = nil;
}
2.注意点:千万不能用NSMutableData来拼接服务器返回的数据
二、NSURLConnection发送异步请求的方法
1.block形式 - 除开大文件下载以外的操作,都可以用这种形式
[NSURLConnection sendAsynchronousRequest:<#(NSURLRequest *)#> queue:<#(NSOperationQueue *)#> completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { }];
2.代理形式 - 一般用在大文件下载
// 1.URL
NSURL *url = [NSURL URLWithString:@"http://localhost:8080/MJServer/login?username=123&pwd=123"];
// 2.请求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// 3.下载(创建完conn对象后,会自动发起一个异步请求)
[NSURLConnection connectionWithRequest:request delegate:self];
【iOS基础】NSURLConnection的更多相关文章
- IOS基础学习-2: UIButton
IOS基础学习-2: UIButton UIButton是一个标准的UIControl控件,UIKit提供了一组控件:UISwitch开关.UIButton按钮.UISegmentedContro ...
- iOS 基础日记-修饰符
今晚随便温习了一下iOS 基础关于修饰符这块的东西,下面简单的来描述一下,其中有的也是在网络学习到的: strong与weak是由ARC新引入的对象变量属性 ARC的解释:ARC引入了新的对象的生命周 ...
- iOS基础问答面试
<简书社区 — Timhbw>iOS基础问答面试题连载(一)-附答案:http://www.jianshu.com/p/1ebf7333808d <简书社区 — Timhbw> ...
- [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)
A.概述 在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能 1.按钮点击后,显示为“已下载”,并且不 ...
- [置顶] IOS 基础入门教程
IOS 基础入门教程 教程列表: IOS 简介 IOS环境搭建 Objective C 基础知识 创建第一款iPhone应用程序 IOS操作(action)和输出口(Outlet) iOS - 委托( ...
- iOS 基础:Frames、Bounds 和 CGGeometry
https://segmentfault.com/a/1190000004695617 原文:<iOS Fundamentals: Frames, Bounds, and CGGeometry& ...
- iOS 基础入门--Bull' Eye 小游戏
说明 Bull's Eye小游戏是http://www.raywenderlich.com/store/ios-apprentice里非常酷的入门demo 跟着该教程一步步做下来便有了 ...
- iOS 基础函数解析 - Foundation Functions Reference
iOS 基础函数解析 - Foundation Functions Reference 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名- ...
- 动画 iOS基础
动画 iOS基础 1. basic animation 基础动画 一个基础动画 在一个开始值和一个结束值之间运动 messageLabel.alpha=0.0; [UIView ani ...
- iOS基础UI控件介绍-Swift版
iOS基础UI控件总结 iOS基础控件包括以下几类: 1.继承自NSObject:(暂列为控件) UIColor //颜色 UIImage //图像 2.继承自UIView: 只能相应手势UIGest ...
随机推荐
- 武汉科技大学ACM:1005: Soapbear and Honey
Problem Description Soapbear is the mascot of WHUACM team. Like other bears, Soapbear loves honey ve ...
- php入门自学小展示
<!doctype html> <html> <head> <title>PHP函数小展示</title> </head> &l ...
- 按钮点击效果jquery
<html><head> <meta charset="UTF-8"> <title>QQ</title> <me ...
- C++快速排序实现(quicksort)
quicksort:分治思想. 分解:数组A[p, r)被划分成两个子数组A[pq) 和 A[q+1, r),使得A[pq)中的每个元素小于等于A[q], A[q]也小于A[q+1r)中的每个元素.q ...
- C语言初学 简单计算器计算加减乘除程序
#include<stdio.h> main() { float a,b; char c; printf("输入表达式如a+(* - /)b:\n"); scanf( ...
- ural 1180 Stone Game
http://acm.timus.ru/problem.aspx?space=1&num=1180 #include <cstdio> #include <cstring&g ...
- 在同一上机器上建立两个SVN服务
最快的三步: 1,SVNADMIN CREATE NAME 2, 改写CONF目录下的相关三个文件. 3,重写一个SVN的启动脚本,指定这个SVN不同的目录及端口号. [general] ### Th ...
- 18个SaaS及其功能评价
SAAS软件及其功能评价1. 360 两个同步功能都不错,却被埋没了2. 够快云3. DBFen4. Seafile5. 坚果云6. DZ7. 百度云8. 1159. 迷你云10. 微云11. Dro ...
- Leetcode:Largest Number详细题解
题目 Given a list of non negative integers, arrange them such that they form the largest number. For e ...
- rm: cannot remove `/home/cn0000/log/formlog.20140417': Read-only file system
[root@localhost home]# su - cn0000 rm: cannot remove `/home/cn0000/log/monitor_xmllog.20140417': Rea ...