The resource could not be loaded because the App Transport Security policy requires the use of a secure connection
xmpp 项目中遇到的问题,用苹果的通信API 写一个PUT 方法,向服务器上传一张图片。遇到如题问题。
Plist 文件没有NSAppTransportSecurity属性 Dic,添加该属性,再添加二级属性NSAllowsArbitraryLoads BOOL YES
苹果文档:https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html
stackOverflow:http://stackoverflow.com/questions/32631184/the-resource-could-not-be-loaded-because-the-app-transport-security-policy-requi
工具类:HTTPTool.h
#import <Foundation/Foundation.h> typedef void (^HttpToolProgressBlock)(CGFloat progress);
typedef void (^HttpToolCompletionBlock)(NSError *error); @interface HttpTool : NSObject -(void)uploadData:(NSData *)data
url:(NSURL *)url
progressBlock : (HttpToolProgressBlock)progressBlock
completion:(HttpToolCompletionBlock) completionBlock; /**
下载数据
*/
-(void)downLoadFromURL:(NSURL *)url
progressBlock : (HttpToolProgressBlock)progressBlock
completion:(HttpToolCompletionBlock) completionBlock; -(NSString *)fileSavePath:(NSString *)fileName; @end
HTTPTool.m
#import "HttpTool.h"
#define kTimeOut 5.0
@interface HttpTool()<NSURLSessionDownloadDelegate,NSURLSessionTaskDelegate>{
//下载
HttpToolProgressBlock _dowloadProgressBlock;
HttpToolCompletionBlock _downladCompletionBlock;
NSURL *_downloadURL;
//上传
HttpToolProgressBlock _uploadProgressBlock;
HttpToolCompletionBlock _uploadCompletionBlock;
}
@end
@implementation HttpTool
#pragma mark - 上传
-(void)uploadData:(NSData *)data url:(NSURL *)url progressBlock:(HttpToolProgressBlock)progressBlock completion:(HttpToolCompletionBlock)completionBlock{
NSAssert(data != nil, @"上传数据不能为空");
NSAssert(url != nil, @"上传文件路径不能为空");
_uploadProgressBlock = progressBlock;
_uploadCompletionBlock = completionBlock;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:kTimeOut];
request.HTTPMethod = @"PUT";
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
//NSURLSessionDownloadDelegate
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
//定义下载操作
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:data];
[uploadTask resume];
}
#pragma mark - 上传代理
#pragma mark - 上传进度
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend{
if (_uploadProgressBlock) {
CGFloat progress = (CGFloat) totalBytesSent / totalBytesExpectedToSend;
_uploadProgressBlock(progress);
}
}
#pragma mark - 上传完成
-(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{
if (_uploadCompletionBlock) {
_uploadCompletionBlock(error);
_uploadProgressBlock = nil;
_uploadCompletionBlock = nil;
}
}
#pragma mark - 下载
-(void)downLoadFromURL:(NSURL *)url
progressBlock:(HttpToolProgressBlock)progressBlock
completion:(HttpToolCompletionBlock)completionBlock{
NSAssert(url != nil, @"下载URL不能传空");
_downloadURL = url;
_dowloadProgressBlock = progressBlock;
_downladCompletionBlock = completionBlock;
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:kTimeOut];
//session 大多数使用单例即可
NSURLResponse *response = nil;
//发达同步请求
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
//NSLog(@"%lld",response.expectedContentLength);
if (response.expectedContentLength <= ) {
if (_downladCompletionBlock) {
NSError *error =[NSError errorWithDomain:@"文件不存在" code: userInfo:nil];
_downladCompletionBlock(error);
//清除block
_downladCompletionBlock = nil;
_dowloadProgressBlock = nil;
}
return;
}
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
//NSURLSessionDownloadDelegate
NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[[NSOperationQueue alloc] init]];
//定义下载操作
NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request];
[downloadTask resume];
}
#pragma mark -NSURLSessionDownloadDelegate
#pragma mark 下载完成
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{
//图片保存在沙盒的Doucument下
NSString *fileSavePath = [self fileSavePath:[_downloadURL lastPathComponent]];
//文件管理
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager copyItemAtURL:location toURL:[NSURL fileURLWithPath:fileSavePath] error:nil];
if (_downladCompletionBlock) {
//通知下载成功,没有没有错误
_downladCompletionBlock(nil);
//清空block
_downladCompletionBlock = nil;
_dowloadProgressBlock = nil;
}
}
#pragma mark 下载进度
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{
if (_dowloadProgressBlock) {
//已写数据字节数除以总字节数就是下载进度
CGFloat progress = (CGFloat)totalBytesWritten / totalBytesExpectedToWrite;
_dowloadProgressBlock(progress);
}
}
-(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes{
}
#pragma mark -传一个文件名,返回一个在沙盒Document下的文件路径
-(NSString *)fileSavePath:(NSString *)fileName{
NSString *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[];
//图片保存在沙盒的Doucument下
return [document stringByAppendingPathComponent:fileName];
}
@end
The resource could not be loaded because the App Transport Security policy requires the use of a secure connection的更多相关文章
- Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Xcode7 beta 网络请求报错:The ...
- The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.问题解决
didFailLoadWithError(): Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loa ...
- 网络请求报错:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
iOS9引入了新特性App Transport Security (ATS).详情:App Transport Security (ATS) 如果你想设置不阻止任何网络,只需要在info.plist文 ...
- AFNetworking 提示"The resource could not be loaded because the App Transport Security policy requires the use of a secure connection" 解决办法
原因:iOS9以后,苹果把原http协议改成了https协议,所以不能直接在http协议下GET/POST 解决方案之一: 直接编辑工程文件下的Info.plist文件,加入以下代码 <key& ...
- xcode9.4 报错 error:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
原因 http | https 协议 不能正常使用 找到的解决方案 但是在字段名上有了变化,不过复制进去 还是会自动选择对应的 解决办法 1. 在Info.plist中添加 App Transpor ...
- IOS9网络请求报错:The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.
今天下载Xcode7试了下,运行项目时报上面的错误,网上查了下原来iOS9引入了新特性App Transport Security (ATS).详情:App Transport Security (A ...
- Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Security policy requir
今天升级Xcode 7.0 bata发现网络访问失败.输出错误信息 The resource could not be loaded because the App Transport S ...
- The resource could not be loaded because the App Transport
Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Xcode7 beta 网络请求报错:The ...
- Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport
Xcode7 beta 网络请求报错:The resource could not be loaded because the App Transport Xcode7 beta 网络请求报错:The ...
随机推荐
- HDU1009老鼠的旅行 (贪心算法)
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Auguse 2nd, Week 32nd Tuesday, 2016
Love me little and love me long.不求情意绵绵,但愿天长地久. Friends are relatives you make for yourself.朋友是你自己结交的 ...
- Codeforces Round #321 (Div. 2)C(tree dfs)
题意:给出一棵树,共有n个节点,其中根节点是Kefa的家,叶子是restaurant,a[i]....a[n]表示i节点是否有猫,问:Kefa要去restaurant并且不能连续经过m个有猫的节点有多 ...
- windows网络版象棋的实现
要构建网络版象棋,首先应该创建服务器与客户端,建立socket连接 1) 开局,你是什么颜色 2)选择棋子, 3)走棋 4)悔棋(悔棋悔两步) 5)认输 网络实现: 1)建立连接 a.主机,建立监听s ...
- SPI试验---verilog(实用单通模式)
SPI通信的读写操作 一. SPI简介: SPI的通信原理很简单,它以主从方式工作,这种模式通常有一个主设备和一个或多个从设备,需要至少4根线,事实上3根也可以(单向传输时).也是所有基于SP ...
- grep' \b\b'
\b单词锁定符,如: '\bgrep\b'只匹配grep [root@86 ttf-arphic-uming-0.0.20050501]# cat /proc/diskstats 1 0 ram0 0 ...
- IIS7 / IIS7.5 URL 重写 HTTP 重定向到 HTTPS(转)
转自: http://www.cnblogs.com/yipu/p/3880518.html 1.购买SSL证书,参考:http://www.cnblogs.com/yipu/p/3722135. ...
- 菜鸟学Linux命令:tail命令 查看日志
tail 命令用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理. tail命令常用来查看日志文件.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filenam ...
- Linux USB驱动
linux usb 驱动详解 一 http://blog.163.com/cl2006ky@126/blog/static/87195173201131245557340/ USB设备驱动开发-USB ...
- ***微信LBS地理位置开发+百度地图API(地理位置和坐标转换)
微信公众平台开发 - 获取用户地理位置 本文介绍在微信公众平台上如何使用高级接口开发获取用户地理位置的功能. 一.获取用户地理位置接口 开通了上报地理位置接口的公众号,用户在关注后进入公众号会话时,会 ...