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 ...
随机推荐
- 浅谈c标签
今天用c便签,没有写好,以为jsp不兼容c标签,最后发现,原来是c标签写错了,好吧,简单写两句. jsp要用c标签,首先要导入c标签库,导入jstl.jar与standard.jar两个jar包,一般 ...
- windows8输入法终极完美修复
现在WIN8正式版出现以来,win8的用户越来越多,毕竟是新系统,BUG肯定是有的,现在小编就为大家解决一个大BUG. 输入法BUG: 现象:1.删除系统输入法,重启后无法调出输入法; 2.卸载用户安 ...
- opencv学习笔记(五)镜像对称
opencv学习笔记(五)镜像对称 设图像的宽度为width,长度为height.(x,y)为变换后的坐标,(x0,y0)为原图像的坐标. 水平镜像变换: 代码实现: #include <ios ...
- Stanford大学机器学习公开课(五):生成学习算法、高斯判别、朴素贝叶斯
(一)生成学习算法 在线性回归和Logistic回归这种类型的学习算法中我们探讨的模型都是p(y|x;θ),即给定x的情况探讨y的条件概率分布.如二分类问题,不管是感知器算法还是逻辑回归算法,都是在解 ...
- C语言实现大数据除法
本题要求计算A/B,其中A是不超过1000位的正整数,B是1位正整数.你需要输出商数Q和余数R,使得A = B * Q + R成立. 输入格式: 输入在1行中依次给出A和B,中间以1空格分隔. 输出格 ...
- an error occurred during the file system check错误的解决
[root@GIT ~]# fsck -A /dev/mapper/VolGroup-lv_root 下面的选择,一路Y就行了,最后reboot,问题解决!
- 理解LoadRunner中的局部变量和全局变量
在C语言中,变量有效性的范围称变量的作用域.不仅对于形参变量,C语言中所有的量都有自己的作用域,按作用域范围不同,可分为局部变量和全局变量两种变量类型. 在LoadRunner中应用全局变量和局部变量 ...
- 1、揭秘通用平台的 HttpClient (译)
原文链接:Demystifying HttpClient APIs in the Universal Windows Platform 正打算翻译这篇文章时,发现园子里已经有朋友翻译过了,既然已经开始 ...
- 在crontab中动态写日志
45 3 * * * setsid script -c /home/dlht/shell/coreBusiness/coreOpt.sh >> /home/dlht/logs/coreO ...
- XmlBeanFactory的Bean加载
如何使用这些bean,bean加载的探索: MyTestBean bean=(MyTestBean) bf.getBean("myTestBean"); AbstractBeanF ...