46 ios nsurlsession nsurlsessionuploadtask
我NSURLSession用于背景图片上传.根据上传的图像,我的服务器给了我回复,我相应地更改了我的应用程序.但是当我的应用程序在后台上传图片时,我无法获得服务器响应,因为没有完成块.
有没有办法在不使用完成块的情况下获得响应NSURLUploadTask?
这是我的代码:
self.uploadTask = [self.session uploadTaskWithRequest:request fromData:body completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"returnString : %@",returnString);
NSLog(@"error : %@",error);
}];
[self.uploadTask resume];
Run Code Online (Sandbox Code Playgroud)
但我得到了这个错误..
由于未捕获的异常'NSGenericException'而终止应用程序,原因:'后台会话不支持完成处理程序块.请改为使用委托.
但是,如果我不能使用完成处理程序,我应该如何获得服务器响应.它说使用委托,但我找不到任何可以给我服务器响应的委托方法.
Rob*_*Rob 77
几点想法:
首先,用a实例化你的会话delegate,因为后台会话必须有一个委托:
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:kSessionIdentifier];
self.session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
Run Code Online (Sandbox Code Playgroud)
其次,在NSURLSessionUploadTask没有完成处理程序的情况下实例化你,因为添加到后台会话的任务不能使用完成块.另请注意,我使用的是文件URL,而不是NSData:
NSURLSessionTask *task = [self.session uploadTaskWithRequest:request fromFile:fileURL];
[task resume];
Run Code Online (Sandbox Code Playgroud)
第三,实施相关的委托方法.至少,这可能看起来像:
- (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(@"%@ failed: %@", task.originalRequest.URL, error);
}
NSMutableData *responseData = self.responsesData[@(task.taskIdentifier)];
if (responseData) {
// my response is JSON; I don't know what yours is, though this handles both
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
if (response) {
NSLog(@"response = %@", response);
} else {
NSLog(@"responseData = %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}
[self.responsesData removeObjectForKey:@(task.taskIdentifier)];
} else {
NSLog(@"responseData is nil");
}
}
Run Code Online (Sandbox Code Playgroud)
注意,上面是利用先前实例化的NSMutableDictionary被调用responsesData(因为,令我很懊恼的是,这些"任务"委托方法是在"会话"级别完成的).
最后,您要确保定义一个属性来存储completionHandler提供者handleEventsForBackgroundURLSession:
@property (nonatomic, copy) void (^backgroundSessionCompletionHandler)(void);
Run Code Online (Sandbox Code Playgroud)
显然,请让您的应用代表响应handleEventsForBackgroundURLSession,保存completionHandler,将在下面的URLSessionDidFinishEventsForBackgroundURLSession方法中使用.
- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler {
// This instantiates the `NSURLSession` and saves the completionHandler.
// I happen to be doing this in my session manager, but you can do this any
// way you want.
[SessionManager sharedManager].backgroundSessionCompletionHandler = completionHandler;
}
Run Code Online (Sandbox Code Playgroud)
然后确保NSURLSessionDelegate在后台会话完成时在主线程上调用此处理程序:
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {
if (self.backgroundSessionCompletionHandler) {
dispatch_async(dispatch_get_main_queue(), ^{
self.backgroundSessionCompletionHandler();
self.backgroundSessionCompletionHandler = nil;
});
}
}
Run Code Online (Sandbox Code Playgroud)
仅在某些上传在后台完成时才会调用此方法.
正如你所看到的,有一些活动部件,但这基本上就是需要的.
| 归档时间: |
|
| 查看次数: |
30263 次 |
| 最近记录: |