0

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?

LettersBa
  • 747
  • 1
  • 8
  • 27
  • Apple will almost certainly reject your app if it has the external accessory flag set but doesn't use an external accessory. Use Douglass Hill's suggestion. – Duncan C Feb 16 '15 at 12:33
  • @DuncanC If I use NSUrlSession with the external accessory flag set, My app will enter in app store? – LettersBa Feb 16 '15 at 12:38
  • Not unless your app actually connects to an external accessory. When you set one of the flags asking for special behavior, Apple makes you prove that your app really needs that flag. – Duncan C Feb 16 '15 at 13:40
  • If you use NSURLSession then you don't need the external accessory flag. NSURLSession has system support for downloading even when your app isn't running. It is what you want. – Duncan C Feb 16 '15 at 13:41

1 Answers1

2

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.

Douglas Hill
  • 1,537
  • 10
  • 14
  • 1
    Plus 1 for NSURLSession. That's what it's designed for. It will give you background downloading even when your app isn't running. Much better choice. – Duncan C Feb 16 '15 at 12:32
  • If I use the NSUrlSession inside my method sendFilesToServer, and continue not call the method endBackgroundUpdateTask, all things will be ok? – LettersBa Feb 16 '15 at 12:37
  • You do not need to use the background task API if you are using a properly configured `NSURLSession`. This is not a quick fix as the API is very different, but it is the right way. You should start by reading and understanding the documentation. – Douglas Hill Feb 16 '15 at 12:43