0

I have two views: myFristView and mySecondView, in myFristView I have a code which can read the fields in my database and then send to a PHP server, this code NSInvocationOperation using this class to send the files in background mode.

The process of sending files to the server is a bit slow (because there are many records in the database), you are often forced to wait all files being sent to get into mySecondView, this occurred because besides I be using ARC, the previous view stops working and the app focuses on the second view, making stops sending files.

So I would like to find a way to: when I leave the screen (myFristView), the application continues to run the command in which sends the files to the PHP server.

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);

}
user3781174
  • 245
  • 5
  • 16

1 Answers1

0

I am using this for my video upload.

First declare the background task property:

@property UIBackgroundTaskIdentifier backgroundUploadingID;

Setup background task:

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;
         }
       });
}];

Don't forget to disable it when the task is finished:

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