LettersBa
LettersBa

Reputation: 767

Run network connection in background mode

I have this code who send data and receive even when the application is in background mode (minimized app):

MyViewController.m

    -(void)viewDidAppear:(BOOL)animated{
        [self doUpdateEvenAppMinimized];
    }

    - (void) doUpdate{

        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

            [self beginBackgroundUpdateTask];

            [self sendFilesToServer];//Inside this method have a sleep that call every 5 minutes

//The code used in sendFilesToServer is the same in this website https://dcraziee.wordpress.com/2013/05/29/how-to-upload-file-on-server-in-objective-c/

            //[self endBackgroundUpdateTask];//This method is forever...so I not need to call this line
        });

    }
    - (void)beginBackgroundUpdateTask{ 
    self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{   
    [self endBackgroundUpdateTask];
    }];
    }

    - (void) endBackgroundUpdateTask{

    [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
    self.backgroundUpdateTask = UIBackgroundTaskInvalid;

    }

The documentation says that the maximum time is 10 minutes, and to remove it I use the concept of Implementing Long-Running Tasks, For that I select my project > capabilities > Background Modes (Turn On) > External accessory communication (Checked).

With these steps, my application will be exempt from the 10 minutes?

Upvotes: 0

Views: 614

Answers (1)

Douglas Hill
Douglas Hill

Reputation: 1547

Trying to circumvent the rules to run in the background sounds like the wrong approach. Consider using NSURLSession for long running networking operations that are not tied to the lifetime of your app.

Upvotes: 2

Related Questions