Objective c NSURLSession确保上传工作正常
确保上传工作的最佳方法是什么 我需要上传图像到服务器,并确保上传工作正常。如果由于任何原因,上传不起作用,我将不得不稍后重试 目前,我正在使用NSUrlSession上载图像:Objective c NSURLSession确保上传工作正常,objective-c,ios7,upload,nsurlsession,Objective C,Ios7,Upload,Nsurlsession,确保上传工作的最佳方法是什么 我需要上传图像到服务器,并确保上传工作正常。如果由于任何原因,上传不起作用,我将不得不稍后重试 目前,我正在使用NSUrlSession上载图像: - (void)didCreateSignature:(UIImage *)image { BLog(); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES
- (void)didCreateSignature:(UIImage *)image {
BLog();
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imageFile = [NSString stringWithFormat:@"%@/%@", documentsDirectory,@"test.png"];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:imageFile atomically:YES];
while (![[NSFileManager defaultManager] fileExistsAtPath:imageFile]) {
[NSThread sleepForTimeInterval:.5];
}
NSURLSession *session = [self backgroundSession];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:self.uploadUrl]];
[request setHTTPMethod:@"POST"];
[request addValue:@"image/png" forHTTPHeaderField:@"Content-Type"];
NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromFile:[NSURL fileURLWithPath:signatureFile]];
[uploadTask resume];
}
现在,假设用户没有internet连接,那么将触发代理:
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
BLog();
if (error == nil)
{
NSLog(@"Task: %@ completed successfully", task);
}
else
{
NSLog(@"Task: %@ completed with error: %@", [task originalRequest], [error localizedDescription]);
dispatch_async(dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];
self.internetReachability = [Reachability reachabilityForInternetConnection];
[self.internetReachability startNotifier];
});
}
}
根据Apple文档,如果可达性状态已更改,您应该稍后重试。因此,我考虑将这些任务添加到阵列中,并在互联网连接可用后再次启动每个任务
这是正确的方法吗
如果用户关闭应用程序(通过TaskManager),会发生什么情况。如何确保恢复这些任务?同时,我使用自定义UploadManager类解决了这个问题,并将下载内容存储在核心数据中,直到上传成功:(下面的代码不完整,只显示了基本概念。如果有人对完整实现感兴趣,请告诉我) 在应用程序委托中插入以下内容:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Try to reupload all tasks in DB
dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(backgroundQueue, ^{
[self.uploadManager uploadTasksInDatabase];
});
return YES;
}
这样,每当应用程序再次启动时,它将尝试重新加载所有未完成的任务。我使用这段代码是因为uploadTaskWithRequest:fromData:对我来说根本不起作用(),但这个API fromFile:由于某种原因最终创建了一个大文件,php错误日志文件说13M文件超过了8M限制。我知道我可以提高服务器限制,但为什么它会使文件如此大?图像数据来自哪里?你试过压缩图像了吗:明白了,我用的是PNGRepresentation。我切换到JPEGRepresentation并添加了一个压缩参数0.6,它成功了。但是我仍然无法使用fromData或fromFile上传任何文件。这就是问题:)我疯了,因为我没有收到任何错误。我从服务器返回HTTP代码200,但没有上传任何内容。服务器报告什么:var_dump($_FILES)?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Try to reupload all tasks in DB
dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(backgroundQueue, ^{
[self.uploadManager uploadTasksInDatabase];
});
return YES;
}