如何做一家网站,如何查网站是哪家公司做的,wordpress pjax,物联网学什么NSURLSession后台上传的基本逻辑是#xff1a;首先创建一个后台模式的NSURLSessionConfiguration#xff0c;然后通过这个configuration创建一个NSURLSession#xff0c;接着是创建相关的NSURLSessionTask#xff0c;最后就是处理相关的代理事件。
1、创建NSURLSession
-…NSURLSession后台上传的基本逻辑是首先创建一个后台模式的NSURLSessionConfiguration然后通过这个configuration创建一个NSURLSession接着是创建相关的NSURLSessionTask最后就是处理相关的代理事件。
1、创建NSURLSession
- (NSURLSession *)backgroundURLSession {static NSURLSession *session nil;static dispatch_once_t onceToken;dispatch_once(onceToken, ^{NSURLSessionConfiguration* sessionConfig nil;NSString *identifier [NSString stringWithFormat:%.%, [NSBundle mainBundle].bundleIdentifier, HttpUrlManager];sessionConfig [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:identifier];//请求的缓存策略sessionConfig.requestCachePolicy NSURLRequestUseProtocolCachePolicy;//数据传输超时当恢复传输时会清零sessionConfig.timeoutIntervalForRequest 60;//单条请求超时决定一条请求的最长生命周期sessionConfig.timeoutIntervalForResource 60;//请求的服务类型sessionConfig.networkServiceType NSURLNetworkServiceTypeDefault;//是否允许使用移动网络电话网络default is YESsessionConfig.allowsCellularAccess YES;//后台模式生效YES允许自适应系统性能调节sessionConfig.discretionary YES;session [NSURLSession sessionWithConfiguration:sessionConfig delegate:self delegateQueue:nil];});return session;
}
NSURLSessionConfiguration配置有三种模式
//默认模式类似于原来的NSURLConnection可以使用缓存的CacheCookie鉴权(NSURLSessionConfiguration *)defaultSessionConfiguration;//及时模式不使用缓存的CacheCookie鉴权(NSURLSessionConfiguration *)ephemeralSessionConfiguration;//后台模式在后台完成上传下载创建Configuration对象的时候需要给一个NSString的ID用于追踪完成工作的Session是哪一个(NSURLSessionConfiguration *)backgroundSessionConfigurationWithIdentifier:(NSString *)identifier
2、后台上传
- (void)upload:(NSString *)urlStr data:(NSData *)data headers:(NSDictionary *)headers parameters:(NSDictionary *)parameters name:(NSString *)name filename:(NSString *)filename mimeType:(NSString *)mimeType success:(void (^)(id responseObject))success failure:(void (^)(int code, NSString *message))failure {NSURL *url [NSURL URLWithString:urlStr];NSMutableURLRequest *request [NSMutableURLRequest requestWithURL:url];request.HTTPMethod POST;NSString *string [NSString stringWithFormat:multipart/form-data; charsetutf-8; boundary%, kBoundary];[request setValue:string forHTTPHeaderField:Content-Type];if (headers ! nil) {for (NSString *key in headers.allKeys) {[request setValue:headers[key] forHTTPHeaderField:key];}}NSData *bodyData [self bodyFormData:data parameters:parameters name:name filename:filename mimeType:mimeType];NSString *tempPath NSTemporaryDirectory();NSTimeInterval interval [NSDate.now timeIntervalSince1970];NSString *tempName [NSString stringWithFormat:temp%.0f_%, interval, filename];NSString *tempPath [tempPath stringByAppendingPathComponent:tempName];[bodyData writeToFile:tempPath atomically:YES];NSURLSession *session self.backgroundURLSession;NSURLSessionUploadTask *uploadTask [session uploadTaskWithRequest:request fromFile:[NSURL fileURLWithPath:tempPath]];[uploadTask resume];
}- (NSData *)bodyFormData:(NSData *)data parameters:(NSDictionary *)parameters name:(NSString *)name filename:(NSString *)filename mimeType:(NSString *)mimeType {if (data nil || data.length 0) {return nil;}NSMutableData *formData [NSMutableData data];NSData *lineData [\r\n dataUsingEncoding:NSUTF8StringEncoding];NSData *boundary [[NSString stringWithFormat:--%, kBoundary] dataUsingEncoding:NSUTF8StringEncoding];if (parameters ! nil) {[parameters enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {[formData appendData:boundary];[formData appendData:lineData];NSString *thisFieldString [NSString stringWithFormat:Content-Disposition: form-data; name\%\\r\n\r\n%, key, obj];[formData appendData:[thisFieldString dataUsingEncoding:NSUTF8StringEncoding]];[formData appendData:lineData];}];}[formData appendData:boundary];[formData appendData:lineData];NSString *thisFieldString [NSString stringWithFormat:Content-Disposition: form-data; name\%\; filename\%\\r\nContent-Type: %, name, filename, mimeType];[formData appendData:[thisFieldString dataUsingEncoding:NSUTF8StringEncoding]];[formData appendData:lineData];[formData appendData:lineData];[formData appendData:data];[formData appendData:lineData];[formData appendData:[[NSString stringWithFormat:--%--\r\n, kBoundary] dataUsingEncoding:NSUTF8StringEncoding]];return formData;
}上传有4种方法
/* Creates an upload task with the given request. The body of the request will be created from the file referenced by fileURL */
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL;/* Creates an upload task with the given request. The body of the request is provided from the bodyData. */
- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromData:(NSData *)bodyData;- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler;- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromData:(nullable NSData *)bodyData completionHandler:(void (^)(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error))completionHandler;后台模式不支持使用带回调的上传方法否则会报错 Completion handler blocks are not supported in background sessions. Use a delegate instead. 后台模式不支持使用NSData的上传方法否则会报错 Upload tasks from NSData are not supported in background sessions 所以如果使用后台模式上传选择uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL方法。
NSURLSessionDataDelegate上传代理事件
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {NSLog(URLSession didSendBodyData progress: %f ,totalBytesSent/(float)totalBytesExpectedToSend);
}- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {NSLog(%s, __func__);
}- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {NSMutableData *responseData self.responsesData[(dataTask.taskIdentifier)];if (!responseData) {responseData [NSMutableData dataWithData:data];self.responsesData[(dataTask.taskIdentifier)] responseData;} else {[responseData appendData:data];}
}- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {if (error) {NSLog(URLSession didCompleteWithError % failed: %, task.originalRequest.URL, error);}NSMutableData *responseData self.responsesData[(task.taskIdentifier)];if (responseData) {NSDictionary *response [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];if (response) {NSLog(response %, response);} else {NSString *errMsg [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];NSLog(responseData %, errMsg);}[self.responsesData removeObjectForKey:(task.taskIdentifier)];} else {NSLog(responseData is nil);}
}3、后台请求
- (void)request:(NSString *)urlStr method:(NSString *)method headers:(NSDictionary *)headers parameters:(NSDictionary *)parameters success:(void (^)(id responseObject))success failure:(void (^)(int code, NSString *message))failure {urlStr [self getFullUrlString:urlStr parameters:parameters];NSURL *url [NSURL URLWithString:urlStr];NSMutableURLRequest *request [NSMutableURLRequest requestWithURL:url];request.HTTPMethod method;if (headers ! nil) {for (NSString *key in headers.allKeys) {[request setValue:headers[key] forHTTPHeaderField:key];}}NSURLSession *session self.backgroundURLSession;NSURLSessionDataTask *task [session dataTaskWithRequest:request];[task resume];
}- (NSString *)getFullUrlString:(NSString *)urlStr parameters:(NSDictionary *)parameters {NSMutableString *newStr [NSMutableString stringWithString:urlStr];if (parameters.allKeys.count 0) {BOOL isFirst NO;for (NSString *key in parameters) {isFirst YES;[newStr appendString:isFirst??:];[newStr appendFormat:%%, key, parameters[key]];}}return newStr;
}
4、Session和ApplicationDelegate交互
使用BackgroundSession后台模式在Task执行的时候当用户切到后台时Session会和ApplicationDelegate做交互在BackgroundSession中的Task还会继续下载/上传。
现在分三个场景分析下Session和Application的关系
1当加入了多个Task程序没有切换到后台。
这种情况Task会按照NSURLSessionConfiguration的设置正常下载不会和ApplicationDelegate有交互。
2当加入了多个Task程序切到后台所有Task都完成下载
在切到后台之后Session的Delegate不会再收到Task相关的消息直到所有Task全都完成后系统会调用ApplicationDelegate的application:handleEventsForBackgroundURLSession:completionHandler:回调之后“汇报”下载工作对于每一个后台下载的Task调用Session的Delegate中的URLSession:downloadTask:didFinishDownloadingToURL:成功的话和URLSession:task:didCompleteWithError:成功或者失败都会调用。
AppDelegate
property (copy, nonatomic) void(^backgroundSessionCompletionHandler)();- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler {self.backgroundSessionCompletionHandler completionHandler;
}
Session的Delegate
interface MyViewController()NSURLSessionDelegate
endimplementation MyViewController- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {AppDelegate *appDelegate (AppDelegate *)[[UIApplication sharedApplication] delegate];if (appDelegate.backgroundSessionCompletionHandler) {void (^completionHandler)() appDelegate.backgroundSessionCompletionHandler;appDelegate.backgroundSessionCompletionHandler nil;completionHandler();}NSLog(All tasks are finished);
}end
3当加入了多个Task程序切到后台下载完成了几个Task然后用户又切换到前台。程序没有退出
切到后台之后Session的Delegate仍然收不到消息。在下载完成几个Task之后再切换到前台系统会先汇报已经下载完成的Task的情况然后继续下载没有下载完成的Task后面的过程同第一种情况。
4当加入了多个Task程序切到后台几个Task已经完成但还有Task还没有下载完的时候关掉强制退出程序然后再进入程序的时候。程序退出了
由于程序已经退出了后面没有下完Session就不在了后面的Task肯定是失败了。但是已经下载成功的那些Task新启动的程序也没有听“汇报”的机会了。经过实验发现这个时候之前在NSURLSessionConfiguration设置的NSString类型的ID起作用了当ID相同的时候一旦生成Session对象并设置Delegate马上可以收到上一次关闭程序之前没有汇报工作的Task的结束情况成功或者失败。