0

Ok , im not getting answers about this. :(

Multipeer Connectivity audio streaming stop work on background

What about this?

i'm trying to run this code on background.

- (void)applicationDidEnterBackground:(UIApplication *)application {

    __block UIBackgroundTaskIdentifier bgTask = [application beginBackgroundTaskWithExpirationHandler:^{

       [application endBackgroundTask:bgTask];
       bgTask = UIBackgroundTaskInvalid;
    }];


    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        NSTimer *aTimer = [NSTimer timerWithTimeInterval:3
                                                  target:self
                                                selector:@selector(showInformation)
                                                userInfo:nil 
                                                 repeats:YES];

        [[NSRunLoop mainRunLoop]
           addTimer:aTimer
           forMode:NSDefaultRunLoopMode];

        [application endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    });

}

Obviously i ihave this function defined on the same scope

- (void) showInformation {
  NSLog(@"showInformation is called.");
}

But when i put the app on background, the interval message, stop work and when i come back to the foreground continue working ........

This means is not running on the background?.

Is this possible? or am I trying to do something stupidly impossible?

I really appreciate some help.

Thanks a lot.

Community
  • 1
  • 1
Gonzalo Bahamondez
  • 1,371
  • 1
  • 16
  • 37

1 Answers1

1

Regardless your code works or not, your background task will be terminated after a while (>10 minutes) by iOS unless the UIBackgroundModes is set in your app (VOIP, Location service, Audio ..).

For more about Background Execution check Background Execution and Multitasking.

Another option in iOS7 is using Background Fetch, but you don't have control over time (there is a smart Timer used by the iOS).

For better understanding check Raywenderlich's Background Modes in iOS Tutorial.

And if you need something working check the below SO posts:

How do I get a background location update every n minutes in my iOS application?

Running iOS App in background for more than 10 minutes

Will iOS launch my app into the background if it was force-quit by the user?

Community
  • 1
  • 1
Tarek Hallak
  • 18,422
  • 7
  • 59
  • 68
  • Ok, i understand , I was reading all this post and not works for me, this not work 10 minutes, this never run in background, I have enabled VOIP , any more ideas? Thanks – Gonzalo Bahamondez Feb 20 '14 at 11:09
  • OK man I just wanted to save your time ;), VOIP works fine for me, but be careful your app should be VOIP or it gets rejected, this solution should work for you http://stackoverflow.com/a/4808049/1262634 – Tarek Hallak Feb 20 '14 at 18:43
  • When you execute the code with VOIP background mode enabled? you receive output on background mode? ..... – Gonzalo Bahamondez Feb 21 '14 at 19:17
  • The code in the question print something each 3 seconds, but when you put the app on background stop printing. The question is. Exist some way to keep printing the output on background mode, for some time? – Gonzalo Bahamondez Feb 22 '14 at 15:02