IOS开发入门之iOS NSURLSessionDownloadTask 报错
凌雪 2018-11-01 来源 :网络 阅读 996 评论 0

摘要:本文将带你了解IOS开发入门iOS NSURLSessionDownloadTask 报错 Invalid resume data for background download. Background downloads mu,希望本文对大家学IOS有所帮助。

本文将带你了解IOS开发入门iOS NSURLSessionDownloadTask 报错 Invalid resume data for background download. Background downloads mu,希望本文对大家学IOS有所帮助。

 

       

错误: Invalid resume data for   background download. Background downloads must use http or https and must   download to an accessible   file.

网上好多说这个错误在ios10中出现,如果调试的时候,点暂停下载,再重新发布到手机上,再点击开始下载,也会有这个错误。

解决办法:自定义一个NSURLSession分类,在ios10代替-   (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData   *)resumeData;

NSURLSession+TYCorrectedResumeData.h


   

#import <foundation foundation.h="">

 

@interface NSURLSession   (TYCorrectedResumeData)

 

-   (NSURLSessionDownloadTask *)downloadTaskWithCorrectResumeData:(NSData   *)resumeData;

 

@end</foundation>

   

 

NSURLSession+TYCorrectedResumeData.m




   

//

//    NSURLSession+TYCorrectedResumeData.m

//    TYDownloadManagerDemo

//

//    Created by tanyang on 2016/10/7.

//  Copyright ? 2016年 tany. All rights   reserved.

//

 

#import "NSURLSession+TYCorrectedResumeData.h"

#import <uikit uikit.h="">

 

#define IS_IOS10ORLATER ([[[UIDevice currentDevice]   systemVersion] floatValue] >= 10)

 

@implementation NSURLSession   (TYCorrectedResumeData)

 

-   (NSURLSessionDownloadTask *)downloadTaskWithCorrectResumeData:(NSData   *)resumeData {

    NSString *kResumeCurrentRequest =   @"NSURLSessionResumeCurrentRequest";

    NSString *kResumeOriginalRequest =   @"NSURLSessionResumeOriginalRequest";

     

    NSData *cData =   correctResumeData(resumeData);

    cData =   cData?cData:resumeData;

    NSURLSessionDownloadTask *task = [self   downloadTaskWithResumeData:cData];

    NSMutableDictionary *resumeDic =   getResumeDictionary(cData);

    if (resumeDic) {

        if (task.originalRequest == nil)   {

            NSData *originalReqData =   resumeDic[kResumeOriginalRequest];

            NSURLRequest *originalRequest =   [NSKeyedUnarchiver unarchiveObjectWithData:originalReqData   ];

            if (originalRequest) {

                [task setValue:originalRequest   forKey:@"originalRequest"];

            }

        }

        if (task.currentRequest == nil) {

            NSData *currentReqData =   resumeDic[kResumeCurrentRequest];

            NSURLRequest *currentRequest =   [NSKeyedUnarchiver unarchiveObjectWithData:currentReqData];

            if (currentRequest) {

                [task setValue:currentRequest forKey:@"currentRequest"];

            }

        }

         

    }

    return task;

}

 

#pragma mark- private

 

NSData * correctRequestData(NSData *data)   {

    if (!data) {

        return nil;

    }

    // return the same data if it's   correct

    if ([NSKeyedUnarchiver unarchiveObjectWithData:data] != nil)   {

        return data;

    }

    NSMutableDictionary *archive =   [[NSPropertyListSerialization propertyListWithData:data   options:NSPropertyListMutableContainersAndLeaves format:nil error:nil]   mutableCopy];

     

    if (!archive) {

        return nil;

    }

    NSInteger k = 0;

    id objectss = archive[@"$objects"];

    while ([objectss[1] objectForKey:[NSString stringWithFormat:@"$%ld",k]] != nil)   {

        k += 1;

    }

    NSInteger i = 0;

    while ([archive[@"$objects"][1]   objectForKey:[NSString stringWithFormat:@"__nsurlrequest_proto_prop_obj_%ld",i]] != nil)   {

        NSMutableArray *arr =   archive[@"$objects"];

        NSMutableDictionary *dic =   arr[1];

        id obj = [dic objectForKey:[NSString   stringWithFormat:@"__nsurlrequest_proto_prop_obj_%ld",i]];

        if (obj) {

            [dic setValue:obj forKey:[NSString   stringWithFormat:@"$%ld",i+k]];

            [dic removeObjectForKey:[NSString   stringWithFormat:@"__nsurlrequest_proto_prop_obj_%ld",i]];

            [arr   replaceObjectAtIndex:1 withObject:dic];

            archive[@"$objects"] = arr;

        }

        i++;

    }

    if ([archive[@"$objects"][1]   objectForKey:@"__nsurlrequest_proto_props"] != nil) {

        NSMutableArray *arr =   archive[@"$objects"];

        NSMutableDictionary *dic =   arr[1];

        id obj = [dic   objectForKey:@"__nsurlrequest_proto_props"];

        if (obj) {

            [dic setValue:obj forKey:[NSString   stringWithFormat:@"$%ld",i+k]];

            [dic removeObjectForKey:@"__nsurlrequest_proto_props"];

            [arr   replaceObjectAtIndex:1   withObject:dic];

            archive[@"$objects"] = arr;

        }

    }

    // Rectify weird   "NSKeyedArchiveRootObjectKey" top key to   NSKeyedArchiveRootObjectKey =   "root"

    if ([archive[@"$top"] objectForKey:@"NSKeyedArchiveRootObjectKey"] != nil) {

        [archive[@"$top"] setObject:archive[@"$top"][@"NSKeyedArchiveRootObjectKey"] forKey:   NSKeyedArchiveRootObjectKey];

        [archive[@"$top"] removeObjectForKey:@"NSKeyedArchiveRootObjectKey"];

    }

    // Reencode archived   object

    NSData *result = [NSPropertyListSerialization   dataWithPropertyList:archive format:NSPropertyListBinaryFormat_v1_0   options:0   error:nil];

    return result;

}

 

NSMutableDictionary *getResumeDictionary(NSData *data)   {

    NSMutableDictionary *iresumeDictionary =   nil;

    if (IS_IOS10ORLATER) {

        id root =   nil;

        id  keyedUnarchiver =   [[NSKeyedUnarchiver alloc]   initForReadingWithData:data];

        @try {

            root = [keyedUnarchiver   decodeTopLevelObjectForKey:@"NSKeyedArchiveRootObjectKey" error:nil];

            if (root == nil) {

                root = [keyedUnarchiver   decodeTopLevelObjectForKey:NSKeyedArchiveRootObjectKey error:nil];

            }

        } @catch(NSException *exception) {

             

        }

        [keyedUnarchiver finishDecoding];

        iresumeDictionary = [root   mutableCopy];

    }

     

    if (iresumeDictionary == nil) {

        iresumeDictionary =   [NSPropertyListSerialization propertyListWithData:data   options:NSPropertyListMutableContainersAndLeaves format:nil   error:nil];

    }

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标移动开发之IOS频道!

   

本文由 @凌雪 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved