七牛云存储xcode更新为ios9使用七牛的ios sdk上传报错

1,报错信息如下:

<QNResponseInfo= id: 1442654584146535, status: -1022, requestId: (null), xlog: (null), xvia: (null), host: up.qiniu.com ip: (null) duration: 0.181957 s time: 1442654584 error: Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSUnderlyingError=0x7fabd9454190 {Error Domain=kCFErrorDomainCFNetwork Code=-1022 "(null)"}, NSErrorFailingURLStringKey=http://115.231.182.136:80/, NSErrorFailingURLKey=http://115.231.182.136:80/, NSLocalizedDescription=The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.}>

提取出来的报错信息也就是:

"The resource could not be loaded because the App Transport Security policy requires the use of a secure connection."

Google后查证,iOS9引入了新特性App Transport Security (ATS)。详情:App Transport Security (ATS)

新特性要求App内访问的网络必须使用HTTPS协议。
但是现在公司的项目使用的是HTTP协议,使用私有加密方式保证数据安全。现在也不能马上改成HTTPS协议传输。

也就是说ios9只支持https的http链接请求。但是七牛现在上传入口都是http的。所以就报错了。

2,解决方案

先要在info.plist文件中添加NSAppTransportSecurity类型Dictionary。
在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES。然后再去info中再配置。配置如下。

第一步,点击你的项目名称。就会出现右侧的这个配置选项。

第二步,点击info。

第三部,添加NSAppTransportSecurity类型Dictionary。
在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES。

3,上传测试

- (IBAction)qiniuupload:(id)sender {


    QNUploadManager *upManager = [[QNUploadManager alloc] init];
   // NSData *data1 = [@"Hello, World!" dataUsingEncoding : NSUTF8StringEncoding];

    NSURL *fileUrl=[NSURL URLWithString:@"file:///Users/yishiyaonie/Downloads/trim.4C7DE72F-3BEC-4DBB-8FA4-4F11BFDEF0ED.MOV"];

    NSData *data = [NSData dataWithContentsOfURL:fileUrl ];
    [upManager putData:data key:nil token:@"cjBCKBOX4uk0c455PFCH7OJNBnXJg5IdePBaJShL:PLuw18jpgrpn6-kS7uyFLEY2y4Y=:eyJzY29wZSI6Im1lbmdiYW9zaG93IiwiZGVhZGxpbmUiOjE0NDI2NTk1Njh9"
              complete: ^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
                  NSLog(@"%@", info);
                  NSLog(@"七牛返回信息%@", resp);
              } option:nil];
              }

上传结果打印log

2015-09-19 17:52:28.518 qiniu-ios1[4159:225703] FiDa9ASV3HY8ATSv7Jb697YW_lY_
2015-09-19 17:52:29.006 qiniu-ios1[4159:225703] <QNResponseInfo= id: 1442656310635125, status: 200, requestId: uTsAAPHihb9NWAUU, xlog: s.ph;s.put.tw;s.put.tr:10;s.put.tw;s.put.tr:11;s.ph;s.put.tw;s.put.tr:11;s.ph;PFDS:14;PFDS:15;PFDS:17;rs15_6.sel;qtbl.ins/same entry;rs15_6.sel;qtbl.get;MQ;RS.not:;RS:2;rs.put:2;rs-upload.putFile:21;UP:26, xvia: 1.1 changkuan16:5 (Cdn Cache Server V2.0), host: upload.qiniu.com ip: 124.14.6.16 duration: 0.483265 s time: 1442656349 error: (null)>
2015-09-19 17:52:29.006 qiniu-ios1[4159:225703] 七牛返回信息{
    hash = "FiDa9ASV3HY8ATSv7Jb697YW_lY_";
    key = "FiDa9ASV3HY8ATSv7Jb697YW_lY_";
}

然后你拿到返回的key,用你的域名拼接上key,就是你在七牛的url。

你可能感兴趣的:(七牛云存储xcode更新为ios9使用七牛的ios sdk上传报错)