以后台模式上传文件

时间:2014-08-20 14:18:06

标签: objective-c nsoperationqueue nsinvocation nsinvocationoperation

我有两个视图: myFristView mySecondView ,在myFristView中我有一个代码可以读取我的数据库中的字段然后发送到PHP服务器,这段代码 NSInvocationOperation 使用此类以后台模式发送文件。

将文件发送到服务器的过程有点慢(因为数据库中有很多记录),你经常被迫等待所有发送的文件进入mySecondView,这是因为除了我使用ARC ,上一个视图停止工作,应用程序将重点放在第二个视图上,停止发送文件。

所以我想找到一种方法:当我离开屏幕(myFristView)时,应用程序继续运行命令,将文件发送到PHP服务器。

NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(sendFiles) object:nil];
[queue addOperation:operation];

-(void)sendFiles{

                       ......

NSString *urlString = @"http://www.website.com/receiveFiles.php";
    NSString *filename = @"fazerbem.sqlite";
    request= [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    NSMutableData *postbody = [NSMutableData data];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[NSData dataWithData:YOUR_NSDATA_HERE]];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:postbody];

    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"%@", returnString);

}

1 个答案:

答案 0 :(得分:0)

我正在使用此功能进行视频上传。

首先声明后台任务属性:

@property UIBackgroundTaskIdentifier backgroundUploadingID;

设置后台任务:

UIApplication *app = [UIApplication sharedApplication];

self.backgroundUploadingID = [app beginBackgroundTaskWithExpirationHandler:^{

   dispatch_async(dispatch_get_main_queue(), ^{
       if (self.backgroundUploadingID != UIBackgroundTaskInvalid)
       {
               UILocalNotification *alert = [[UILocalNotification alloc] init];
                    [alert setAlertBody:@"Video Upload Timed Out"];
                    [[UIApplication sharedApplication] presentLocalNotificationNow:alert];

                    [app endBackgroundTask:self.backgroundUploadingID];
                    self.backgroundUploadingID = UIBackgroundTaskInvalid;
         }
       });
}];

任务完成后别忘了禁用它:

if (self.backgroundUploadingID != UIBackgroundTaskInvalid)
{
        //  NSLog(@"Ending backgrounding");
        UIApplication *app = [UIApplication sharedApplication];
        [app endBackgroundTask:self.backgroundUploadingID];
        self.backgroundUploadingID = UIBackgroundTaskInvalid;
}