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
1 #import <Foundation/Foundation.h> 2 3 typedef void (^HttpToolProgressBlock)(CGFloat progress); 4 typedef void (^HttpToolCompletionBlock)(NSError *error); 5 6 7 8 @interface HttpTool : NSObject 9 10 -(void)uploadData:(NSData *)data 11 url:(NSURL *)url 12 progressBlock : (HttpToolProgressBlock)progressBlock 13 completion:(HttpToolCompletionBlock) completionBlock; 14 15 /** 16 下载数据 17 */ 18 -(void)downLoadFromURL:(NSURL *)url 19 progressBlock : (HttpToolProgressBlock)progressBlock 20 completion:(HttpToolCompletionBlock) completionBlock; 21 22 23 -(NSString *)fileSavePath:(NSString *)fileName; 24 25 @end
HTTPTool.m
1 #import "HttpTool.h" 2 3 #define kTimeOut 5.0 4 5 6 @interface HttpTool()<NSURLSessionDownloadDelegate,NSURLSessionTaskDelegate>{ 7 8 //下载 9 HttpToolProgressBlock _dowloadProgressBlock; 10 HttpToolCompletionBlock _downladCompletionBlock; 11 NSURL *_downloadURL; 12 13 14 //上传 15 HttpToolProgressBlock _uploadProgressBlock; 16 HttpToolCompletionBlock _uploadCompletionBlock; 17 18 } 19 20 @end 21 22 23 @implementation HttpTool 24 25 26 #pragma mark - 上传 27 -(void)uploadData:(NSData *)data url:(NSURL *)url progressBlock:(HttpToolProgressBlock)progressBlock completion:(HttpToolCompletionBlock)completionBlock{ 28 29 NSAssert(data != nil, @"上传数据不能为空"); 30 NSAssert(url != nil, @"上传文件路径不能为空"); 31 32 _uploadProgressBlock = progressBlock; 33 _uploadCompletionBlock = completionBlock; 34 35 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:kTimeOut]; 36 request.HTTPMethod = @"PUT"; 37 38 NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; 39 40 //NSURLSessionDownloadDelegate 41 NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[[NSOperationQueue alloc] init]]; 42 43 44 //定义下载操作 45 NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:data]; 46 47 [uploadTask resume]; 48 } 49 50 #pragma mark - 上传代理 51 52 53 #pragma mark - 上传进度 54 -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend{ 55 56 if (_uploadProgressBlock) { 57 CGFloat progress = (CGFloat) totalBytesSent / totalBytesExpectedToSend; 58 _uploadProgressBlock(progress); 59 } 60 } 61 62 63 #pragma mark - 上传完成 64 -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error{ 65 if (_uploadCompletionBlock) { 66 _uploadCompletionBlock(error); 67 68 _uploadProgressBlock = nil; 69 _uploadCompletionBlock = nil; 70 } 71 } 72 73 74 #pragma mark - 下载 75 -(void)downLoadFromURL:(NSURL *)url 76 progressBlock:(HttpToolProgressBlock)progressBlock 77 completion:(HttpToolCompletionBlock)completionBlock{ 78 79 80 81 NSAssert(url != nil, @"下载URL不能传空"); 82 83 _downloadURL = url; 84 _dowloadProgressBlock = progressBlock; 85 _downladCompletionBlock = completionBlock; 86 87 88 89 NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:kTimeOut]; 90 91 92 //session 大多数使用单例即可 93 94 NSURLResponse *response = nil; 95 96 97 //发达同步请求 98 [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 99 100 //NSLog(@"%lld",response.expectedContentLength); 101 if (response.expectedContentLength <= 0) { 102 if (_downladCompletionBlock) { 103 NSError *error =[NSError errorWithDomain:@"文件不存在" code:404 userInfo:nil]; 104 _downladCompletionBlock(error); 105 106 //清除block 107 _downladCompletionBlock = nil; 108 _dowloadProgressBlock = nil; 109 } 110 111 return; 112 } 113 114 115 NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; 116 117 118 //NSURLSessionDownloadDelegate 119 NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[[NSOperationQueue alloc] init]]; 120 121 122 //定义下载操作 123 NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithRequest:request]; 124 125 [downloadTask resume]; 126 127 } 128 129 130 #pragma mark -NSURLSessionDownloadDelegate 131 #pragma mark 下载完成 132 -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location{ 133 134 //图片保存在沙盒的Doucument下 135 NSString *fileSavePath = [self fileSavePath:[_downloadURL lastPathComponent]]; 136 137 //文件管理 138 NSFileManager *fileManager = [NSFileManager defaultManager]; 139 [fileManager copyItemAtURL:location toURL:[NSURL fileURLWithPath:fileSavePath] error:nil]; 140 141 if (_downladCompletionBlock) { 142 //通知下载成功,没有没有错误 143 _downladCompletionBlock(nil); 144 145 //清空block 146 _downladCompletionBlock = nil; 147 _dowloadProgressBlock = nil; 148 } 149 150 } 151 152 #pragma mark 下载进度 153 -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite{ 154 155 156 if (_dowloadProgressBlock) { 157 //已写数据字节数除以总字节数就是下载进度 158 CGFloat progress = (CGFloat)totalBytesWritten / totalBytesExpectedToWrite; 159 160 _dowloadProgressBlock(progress); 161 162 } 163 } 164 165 166 -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes{ 167 168 } 169 170 171 #pragma mark -传一个文件名,返回一个在沙盒Document下的文件路径 172 -(NSString *)fileSavePath:(NSString *)fileName{ 173 NSString *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; 174 //图片保存在沙盒的Doucument下 175 return [document stringByAppendingPathComponent:fileName]; 176 } 177 178 @end
来源:https://www.cnblogs.com/wjw-blog/p/5640622.html