Ios 在后台使用NSURLSessionUploadTask上载文件
我正在使用NSURLSessionPloadTask上载多个文件。我想在后台运行相同的进程,它可以很好地用于当前文件。在成功上载当前文件之前,应用程序不会挂起。现在,当第二个文件要上传时,它不会开始上传过程,直到我再次启动或应用程序激活。上传不是介于两者之间,这很好。有没有办法在第一次上传完成后开始下一次上传过程。第二次上传也是在后台开始的,但从应用程序进入后台开始,上传时间最长可达3分钟。这里我展示了我的代码:Ios 在后台使用NSURLSessionUploadTask上载文件,ios,background,upload,nsurlsession,nsurlsessionuploadtask,Ios,Background,Upload,Nsurlsession,Nsurlsessionuploadtask,我正在使用NSURLSessionPloadTask上载多个文件。我想在后台运行相同的进程,它可以很好地用于当前文件。在成功上载当前文件之前,应用程序不会挂起。现在,当第二个文件要上传时,它不会开始上传过程,直到我再次启动或应用程序激活。上传不是介于两者之间,这很好。有没有办法在第一次上传完成后开始下一次上传过程。第二次上传也是在后台开始的,但从应用程序进入后台开始,上传时间最长可达3分钟。这里我展示了我的代码: AppDelegate.h @property (nonatomic, copy)
AppDelegate.h
@property (nonatomic, copy) void(^backgroundTransferCompletionHandler)();
AppDelegate.m
- (void)applicationDidEnterBackground:(UIApplication *)application{
__block UIBackgroundTaskIdentifier backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
NSLog(@"Background Time:%f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
[[UIApplication sharedApplication] endBackgroundTask:backgroundTaskIdentifier];
backgroundTaskIdentifier = backgroundTaskIdentifier;
`enter code here`}];
}
-(void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler{
self.backgroundTransferCompletionHandler = completionHandler;
NSLog(@"handleEventsForBackgroundURLSession called");
}
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{
AppDelegate *appDelegate2 = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(@"URLSessionDidFinishEventsForBackgroundURLSession call in app delegate");
[session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
NSLog(@"uploadTasks: %@",uploadTasks);
if ([uploadTasks count] == 0) {
if (appDelegate2.backgroundTransferCompletionHandler != nil) {
// Copy locally the completion handler.
void(^completionHandler)() = appDelegate2.backgroundTransferCompletionHandler;
// Make nil the backgroundTransferCompletionHandler.
appDelegate2.backgroundTransferCompletionHandler = nil;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// Call the completion handler to tell the system that there are no other background transfers.
completionHandler();
NSLog(@"All tasks are finished");
}];
}
}
else{
}
}];
}
UploadView.m
- (NSURLSession *)backgroundSession {
static NSURLSession *session = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSInteger randomNumber = arc4random() % 1000000;
// NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[NSString stringWithFormat:@"com.example.apple-samplecode.SimpleBackgroundTransfer.BackgroundSession%d",(int)randomNumber]];
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[NSString stringWithFormat:@"com.upload.myapp.BackgroundSession%d",(int)randomNumber]];
sessionConfiguration.sessionSendsLaunchEvents = YES;
sessionConfiguration.HTTPMaximumConnectionsPerHost = 1;
// Define the Upload task
[sessionConfiguration setHTTPAdditionalHeaders: @{@"Accept": @"text/html"}];
sessionConfiguration.timeoutIntervalForRequest = 600.0;
// sessionConfiguration.networkServiceType = NSURLNetworkServiceTypeBackground;
// sessionConfiguration.discretionary = YES;
sessionConfiguration.allowsCellularAccess = YES;
session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
});
return session;
}
ViewDidLoad(){
....
self.uploadSession = [self backgroundSession];
....}
-UploadStart(){//here i am calling this function in loop. When first file got uploaded, control comes here for second task and then upload should continue.
self.uploadTask = [self.uploadSession uploadTaskWithRequest:requestUpload fromFile:[NSURL fileURLWithPath:tmpfile]]; // self.uploadTask is an object of NSURLSessionUploadTask
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
}
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
NSLog(@"didReceiveData Task: %@ upload complete", dataTask);
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) self.uploadTask.response;
if (httpResp.statusCode == 200) {
uploadedFiles++; //an int value
if(uploadedFiles==arrUpload.count){
//all files uploaded here
}
else{
//upload next file from here
}
}
}
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
}
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session{
NSLog(@"URLSessionDidFinishEventsForBackgroundURLSession called");
AppDelegate *appDelegate2 = (AppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate2.backgroundTransferCompletionHandler != nil) {
// Copy locally the completion handler.
void(^completionHandler)() = appDelegate2.backgroundTransferCompletionHandler;
// Make nil the backgroundTransferCompletionHandler.
appDelegate2.backgroundTransferCompletionHandler = nil;
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
// Call the completion handler to tell the system that there are no other background transfers.
completionHandler();
NSLog(@"All tasks are finished");
}];
}
}
上面的代码是完全工作,但有问题的下一个文件是并没有开始在后台上传后3分钟的应用程序后台模式
任何帮助都将不胜感激。先谢谢你。。。有关更多说明,请与我联系。您能找到解决方案吗?您可以使用上述代码。它的工作很好。我想在一些间隔上传数据(在后台模式的应用程序)在服务器上有可能吗??如果是,你能提供建议或样品吗?在应用程序后台模式运行3分钟后,在后台上传是否正常?是的,正常。对不起,我没有正常工作的代码。我已经离开了以前的公司。你在网上任何地方都找不到这种代码。你能告诉我如何每隔15分钟使用NSURLSession在服务器上上传数据吗?