Question

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.

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top