您的位置:首页 > 理论基础 > 计算机网络

ios开发之网络数据的下载与上传

2014-01-03 23:04 507 查看
要实现网络数据的下载与上传,主要有三种方式

> NSURLConnection 针对少量数据,使用“GET”或“POST”方法从服务器获取数据,使用“POST”方法向服务器传输数据;

> NSURLSession(ios7.0新推出的) 针对大量数据,可使用“GET”方法实现线程安全的多线程下载,监控下载进度等,也可以使用“PUT”方法实现上传[put 方法存在严重的安全隐患,目前很少有服务器支持此种上传方式];

> AFNetWorking(2.0之前的版本) 对NSURLConnection进行封装的第三方开源框架,实现了大量数据的下载与上传,但是对于线程安全没有较好的控制措施;

推荐用法:对于少量数据(例如平时的网页信息)的下载与上传,直接使用NSURLConnection,对于大量数据的下载使用NSURLSession,对于大量数据的上传使用AFNetWorking

NSURLConnection:

  1> GET

// 1. 定义URL,确定要访问的地址
NSURL *url = [NSURL URLWithString:urlString];
// 2. 定义URLRequest,确定网络访问请求,在GET方法中直接用URL即可
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:2.0f];

//===================================================
NSURLResponse *response = nil;
NSError *error = nil;
// 同步请求的应用场景:例如:网银账户的登录!
// 一定要获取到某个网络返回数据后,才能进行下一步操作的场景!

// 发送同步请求,respone&error要带地址的原因就是为了方法执行后,能够方便使用response&error
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//-------------------------------------------------
// 发送异步请求[有同步则不需要异步,反之亦然]
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 块代码的内容会在网络访问后执行
// 块代码是预先定义好的代码片段,在满足某个条件时执行的。
NSLog(@"%@", [NSThread currentThread]);
}];


  2> POST

// 1. 定义URL,确定要访问的地址
NSURL *url = [NSURL URLWithString:urlString];
// 2. 定义请求,生成数据体添加到请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// 1) 指定网络请求的方法
request.HTTPMethod = @"POST";

// 2) 生成数据体
// * 先生成字符串
NSString *bodyStr = [NSString stringWithFormat:@"username=%@&password=%@", userName, password];
// * 将字符串转换成NSData
request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];

// 提示:POST请求多用于用户登录,或者上传文件,在实际开发中,“POST请求的参数及地址”需要与公司的后端程序员沟通。
// POST同样具备同步和异步方法,与get中同步异步方法相同


NSURLSession

// url
NSURL *url = [NSURL URLWithString:strURL];

// request
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:3.0f];

// session
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];

_downloadTask = [session downloadTaskWithRequest:request];// NSURLSessionDownloadTask *_downloadTask; 成员变量

[_downloadTask resume];


苹果官方提供的Session还是非常给力的,使用也很方便。 让当前控制器成为当前session的代理,并且实现其中的代理方法,便可以对整个下载过程了如指掌

#pragma mark - download代理方法

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
NSLog(@"error- %@", error.localizedDescription);
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
CGFloat percent = (CGFloat)totalBytesWritten / totalBytesExpectedToWrite;

dispatch_async(dispatch_get_main_queue(), ^{

_progress.progress = percent; // 进度
_progressLabel.text = [NSString stringWithFormat:@"当前:%.1fKb/s,进度:%.2f%%", bytesWritten / 1024.0f, percent * 100];
});
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location
{
NSLog(@"%@", location);
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes
{
NSLog(@"%lld", fileOffset);
}


AFNetWorking

下载使用示例代码:

#pragma mark 下载
- (IBAction)download
{
// 1. 建立请求  _httpClient 是AFHTTPClient实例化出来的成员变量
NSURLRequest *request = [_httpClient requestWithMethod:@"GET" path:@"download/Objective-C2.0.zip" parameters:nil];

// 2. 操作
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];

_downloadOperation = op;

// 下载
// 指定文件保存路径,将文件保存在沙盒中
NSArray *docs = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [docs[0] stringByAppendingPathComponent:@"download.zip"];

op.outputStream = [NSOutputStream outputStreamToFileAtPath:path append:NO];

// 设置下载进程块代码
/*
bytesRead                      当前一次读取的字节数(100k)
totalBytesRead                 已经下载的字节数(4.9M)
totalBytesExpectedToRead       文件总大小(5M)
*/
[op setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {

// 设置进度条的百分比
CGFloat precent = (CGFloat)totalBytesRead / totalBytesExpectedToRead;
NSLog(@"%f", precent);

_progressView.progress = precent;
}];

// 设置下载完成操作
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

// 下载完成之后,解压缩文件
/*
参数1:要解结压缩的文件名及路径 path - > download.zip
参数2:要解压缩到的位置,目录    - > document目录
*/
[SSZipArchive unzipFileAtPath:path toDestination:docs[0]];

// 解压缩之后,将原始的压缩包删除
// NSFileManager专门用于文件管理操作,可以删除,复制,移动文件等操作
// 也可以检查文件是否存在
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];

// 下一步可以进行进一步处理,或者发送通知给用户。
NSLog(@"下载成功");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"下载失败");
}];

// 启动下载
[_httpClient.operationQueue addOperation:op];
}


上传示例代码

#pragma mark - 文件上传
- (IBAction)uploadImage
{
/*
此段代码如果需要修改,可以调整的位置

1. 把upload.php改成网站开发人员告知的地址
2. 把file改成网站开发人员告知的字段名
*/
// 1. httpClient->url

// 2. 上传请求POST
NSURLRequest *request = [_httpClient multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
// 在此位置生成一个要上传的数据体
// form对应的是html文件中的表单
/*
参数
1. 要上传的[二进制数据]
2. 对应网站上[upload.php中]处理文件的[字段"file"]
3. 要保存在服务器上的[文件名]
4. 上传文件的[mimeType]
*/
UIImage *image = [UIImage imageNamed:@"头像1"];
NSData *data = UIImagePNGRepresentation(image);

// 在网络开发中,上传文件时,是文件不允许被覆盖,文件重名
// 要解决此问题,
// 可以在上传时使用当前的系统事件作为文件名
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 设置时间格式
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *str = [formatter stringFromDate:[NSDate date]];
NSString *fileName = [NSString stringWithFormat:@"%@.png", str];

[formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"image/png"];
}];

// 3. operation包装的urlconnetion
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"上传完成");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"上传失败->%@", error);
}];

[_httpClient.operationQueue addOperation:op];
}


注意:AFN中数据不是线程安全的,如果使用暂停、继续功能会造成数据混乱,所以在使用AFN时,尽量不要暂停下载或者上传

AFN还可以用来判断网络状态

- (IBAction)checkNetwork:(id)sender
{
// 1. AFNetwork 是根据是否能够连接到baseUrl来判断网络连接状态的
// 提示:最好使用门户网站来判断网络连接状态。
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:url];
_httpClient = client;

[_httpClient setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

// 之所以区分无线和3G主要是为了替用户省钱,省流量
// 如果应用程序占流量很大,一定要提示用户,或者提供专门的设置,仅在无线网络时使用!
switch (status) {
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"无线网络");
break;
case AFNetworkReachabilityStatusReachableViaWWAN:
NSLog(@"3G网络");
break;
case AFNetworkReachabilityStatusNotReachable:
NSLog(@"未连接");
break;
case AFNetworkReachabilityStatusUnknown:
NSLog(@"未知错误");
break;
}
}];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐