iOS NSURLSession 实现网络请求-文件下载-上传-后台下载

来源:互联网 发布:linux cp 一个文件夹 编辑:程序博客网 时间:2024/05/22 13:49

 *  会话NSURLSession

    NSURLConnection通过全局状态来管理cookies, 认证信息等公共资源, 如果两个连接需要使用不同的资源配置情况时就无法解决,
    这个问题在NSURLSession可以解决, NSURLSession同时对应着多个连接, 会话通过工厂方法来创建, 同一个会话中使用相同的状态信息, NSURLSession支持进程三种会话:
    1. defaultSessionConfiguration : 进程内会话 (默认会话), 用来硬盘来缓存数据.
    2. ephemeralSessionConfiguration : 临时的进程内会话(内存), 不会将cookie, 缓存存储到本地, 只会放到内存中, 当应用程序退出后数据也会消失
    3. backgroundSessionConfiguration : 后台会话, 相比默认会话, 该会话会在后台开启一个县城进行网络数据处理

    精准的控制任务的取消, 挂起, 恢复

1. 请求数据

<span style="font-size:18px;">#pragma mark --- 请求数据 request --- session ---- sessionDataTask- (void)loadData{    // 1. 创建url    NSString *urlString = [NSString stringWithFormat:@"url"];    urlString = [urlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];    NSURL *url = [NSURL URLWithString:urlString];    // 2. 创建请求    NSURLRequest *request = [NSURLRequest requestWithURL:url];    // 3. 创建会话 (使用单例初始化, 启动任务)        NSURLSession *session = [NSURLSession sharedSession];        // 会话创建任务    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {                if (!error) {            NSString *dataStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];            NSLog(@"%@", dataStr);        } else {            NSLog(@"error is %@", error.localizedDescription);        }            }];        // 恢复线程, 启动任务    [dataTask resume];}</span>

2. 文件上传

<span style="font-size:18px;">#pragma mark ----- 文件上传- (void)upDataFile{    /**     *  文件上传的时候需要设置请求头中Content-Type类型, 必须使用URL编码,           application/x-www-form-urlencoded:默认值,发送前对所有发送数据进行url编码,支持浏览器访问,通常文本内容提交常用这种方式。     multipart/form-data:多部分表单数据,支持浏览器访问,不进行任何编码,通常用于文件传输(此时传递的是二进制数据) 。     text/plain:普通文本数据类型,支持浏览器访问,发送前其中的空格替换为“+”,但是不对特殊字符编码。     application/json:json数据类型,浏览器访问不支持 。     text/xml:xml数据类型,浏览器访问不支持。          multipart/form-data 必须进行设置,     */    // 1. 创建URL    NSString *urlStr = @"url";    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];    NSURL *url = [NSURL URLWithString:urlStr];    // 2. 创建请求    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];    // 设置请求的为POST    request.HTTPMethod = @"POST";        // 3.构建要上传的数据    NSString *path = [[NSBundle mainBundle] pathForResource:@"123" ofType:@"123"];    NSData *data = [NSData dataWithContentsOfFile:path];        // 设置request的body    request.HTTPBody = data;        // 设置请求 Content-Length    [request setValue:[NSString stringWithFormat:@"%lu", (unsigned long)data.length] forHTTPHeaderField:@"Content-Length"];    // 设置请求 Content-Type    [request setValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@",@"Xia"] forHTTPHeaderField:@"Content-Type"];        // 4. 创建会话    NSURLSession *session = [NSURLSession sharedSession];    NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:data completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {        if (!error) {            // 上传成功        }else {            // 上传失败, 打印error信息            NSLog(@"error --- %@", error.localizedDescription);        }    }];    // 恢复线程 启动任务    [uploadTask resume];    }</span>

3. 文件下载

<span style="font-size:18px;">#pragma mark --- 文件下载/** *  使用NSURLSessionDownloadTask下载文件过程中注意:    下载文件之后会自动保存到一个临时目录中, 需要自己将文件重新放到其他指定的目录中 */- (void)downLoadFile{    // 1. 创建url    NSString *urlStr =[NSString stringWithFormat:@"%@", @"url"];    urlStr = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];    NSURL *Url = [NSURL URLWithString:urlStr];        // 创建请求    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:Url];        // 创建会话    NSURLSession *session = [NSURLSession sharedSession];        NSURLSessionDownloadTask *downLoadTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {        if (!error) {            // 下载成功            // 注意 location是下载后的临时保存路径, 需要将它移动到需要保存的位置            NSError *saveError;            // 创建一个自定义存储路径            NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];            NSString *savePath = [cachePath stringByAppendingPathComponent:@"fileName"];            NSURL *saveURL = [NSURL fileURLWithPath:savePath];                        // 文件复制到cache路径中            [[NSFileManager defaultManager] copyItemAtURL:location toURL:saveURL error:&saveError];            if (!saveError) {                NSLog(@"保存成功");            } else {                NSLog(@"error is %@", saveError.localizedDescription);            }        } else {            NSLog(@"error is : %@", error.localizedDescription);        }    }];    // 恢复线程, 启动任务    [downLoadTask resume];    }</span>

4. 文件的取消下载,挂起,继续下载

<span style="font-size:18px;">#pragma mark -- 取消下载-(void)cancleDownLoad{    [_downloadTask cancel];}#pragma mark --- 挂起下载- (void)suspendDownload{    [_downloadTask suspend];}#pragma mark ---- 恢复继续下载- (void)resumeDownLoad{    [_downloadTask resume];    }</span>

5. NSURLSession的代理方法


<span style="font-size:18px;">#pragma mark ---- downLoadTask 代理方法// 下载过程中 会多次调用, 记录下载进度- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{    // 记录下载进度    }// 下载完成-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{    NSError *error;    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];    NSString *savePath = [cachePath stringByAppendingPathComponent:@"savename"];        NSURL *saveUrl = [NSURL fileURLWithPath:savePath];    // 通过文件管理 复制文件    [[NSFileManager defaultManager] copyItemAtURL:location toURL:saveUrl error:&error];    if (error) {        NSLog(@"Error is %@", error.localizedDescription);    }}// 当调用恢复下载的时候 触发的代理方法 [_downLoadTask resume]-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes{        }#pragma mark --- 任务完成, 不管是否下载成功- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{    }#pragma mark --- session 后台下载完成 之后的操作 (本地通知 或者 更新UI)- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{    AppDelegate *appdelgate = (AppDelegate *)[UIApplication sharedApplication].delegate;        if (appdelgate.backgroundSessionCompletionHandler) {        void (^completionHandle)() = appdelgate.backgroundSessionCompletionHandler;        appdelgate.backgroundSessionCompletionHandler = nil;        completionHandle();    }}</span>

6. 后台下载


 NSURLSession 支持程序的后台下载和上传, 苹果官方将其称之进程之外的上传和下载, 这些任务都交给后台守护线程完成, 而不是应用本身, 及时文件在下载和上传过程中崩溃了也可以继续运行(如果用户强制关闭程序的话, NSURLSession会断开连接)
 *
 *  为了提高用户体验, 下载过程中进度条会一直刷新进度,
    当程序进入后台后, 事实上任务是交给iOS系统来调度的, 具体什么时候下载完成就不知道了,
    如果下载完成之后, 文件下载进度应该在100位置, 由于程序已经在后台无法更新程序UI,
    此时通过应用程序代理 (AppDelegate)方法进行UI更新
 
   

当NSURLSession在后台开启几个任务之后, 如果有其他几个任务完成后系统会调用应用程序的
   

-(void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier 


completionHandler:(void (^)())completionHandler代理方法
   

此方法会包含一个competionHandler (此操作表示应用中完成所有处理工作), 通常我们会保存此对象,
   

直到最后一个任务完成, 此时重新通过会话标识(sessionConfig中设置的)找到相应的会话并调用NSURLSession的

-(void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session代理方法, 在这个方法中通常可以

进行UI更新,并调用completionHandler通知系统已经完成所有的操作


<span style="font-size:18px;">#pragma mark --- 后台下载- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler{    self.backgroundSessionCompletionHandler = completionHandler;}- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{    AppDelegate *appdelgate = (AppDelegate *)[UIApplication sharedApplication].delegate;        if (appdelgate.backgroundSessionCompletionHandler) {        void (^completionHandle)() = appdelgate.backgroundSessionCompletionHandler;        appdelgate.backgroundSessionCompletionHandler = nil;        completionHandle();    }}</span>





1 0
原创粉丝点击