zoukankan      html  css  js  c++  java
  • iOS NSURLSession的使用

    1.NSURLSessionDataTask

     1     // 确定URL
     2     NSString *urlStr = @"http://localhost/试试看";
     3     urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     4     NSURL *url = [NSURL URLWithString:urlStr];
     5     
     6     // 确定request
     7     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:2.0f];
     8     
     9     // 确定会话
    10     NSURLSession *session = [NSURLSession sharedSession];
    11     
    12     // 由会话生成任务
    13     NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    14         NSLog(@"response-- %@",response);
    15         NSLog(@"data--%@",data);
    16         
    17         // 刷新UI,一定要返回主线程去刷新
    18         dispatch_async(dispatch_get_main_queue(), ^{
    19             [self.imageView  setImage:[UIImage imageWithData:data]];
    20         });
    21 
    22     }];
    23     // URLSession默认生成的任务都是挂起状态,需要使用以下语句执行任务
    24     [dataTask resume];

    2.NSURLSessionDownloadTask实现简单的下载功能

     1     // 确定URL
     2     NSString *urlStr = @"http://localhost/试试看";
     3     urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     4     NSURL *url = [NSURL URLWithString:urlStr];
     5     
     6     // 确定request
     7     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:0 timeoutInterval:2.0f];
     8     
     9     // 确定会话
    10     NSURLSession *session = [NSURLSession sharedSession];
    11     
    12     // 由会话生成任务
    13     NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
    14         // 从tmp中拷贝文件到cache
    15         NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    16         NSURL *cachePathURL = [NSURL fileURLWithPath:cachePath];
    17         NSString *fileNameByMD5 = [[[response URL] absoluteString] MD5];
    18         NSURL *fileURL = [cachePathURL URLByAppendingPathComponent: fileNameByMD5];
    19 
    20         NSFileManager *mgr = [NSFileManager defaultManager];
    21         [mgr moveItemAtURL:location toURL:fileURL  error:NULL];
    22         
    23     }];
    24     
    25     [downloadTask resume];
     1 - (NSString *)MD5
     2 {
     3     const char *cStr = [self UTF8String];
     4     unsigned char digest[CC_MD5_DIGEST_LENGTH];
     5 
     6     CC_MD5(cStr, strlen(cStr), digest);
     7 
     8     NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
     9 
    10     for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
    11         [result appendFormat:@"%02x", digest[i]];
    12     }
    13 
    14     return result;
    15 }

    3.NSURLSessionDownloadTask实现复杂的下载功能:断点续传后台下载

    http://blog.csdn.net/majiakun1/article/details/38133789

  • 相关阅读:
    安装 node-sass 的不成功
    input标签附带提示文字(bootstrap里面输入框的两侧同时添加额外元素)
    更改bootstrap的默认样式
    属性font-family:Font property font-family does not have generic default
    let与const命令
    vue之监听事件
    组件复用传值(待解决问题)
    vue之组件注册
    vue之组件理解(一)
    学习整理与细化(2)——HTML VS XHTML
  • 原文地址:https://www.cnblogs.com/oumygade/p/4205634.html
Copyright © 2011-2022 走看看