Maintain a Multipeer Connectivity session in Background via BackgroundTask?
Asked Answered
P

2

7

I am trying to maintain a MultipeerConnectivity "session" when the application enters temporarily in the background, so I thought about using a background task as I've seen few times here ... The problem is I have no idea how to "maintain" the session with the UIBackgroundTask, can someone please post a hint

I don't care about the advertisers/browsers, it's okay to stop them, but I'd like the session to not disconnect as reconnecting is super buggy for the moment.

Paleethnology answered 12/9, 2014 at 5:56 Comment(2)
Have a look at this code. github.com/davidkaminsky/Unplugged/blob/master/Unplugged/… I had this problem with TCP connections, you can keep your app running in the background for a couple of minutes, but if longer you should clean up your connections and stop the background task. Use it purely to keep connections open while the user is out of your app for a short time.Pompey
Thanks, I ended up doing something a little bit similar, but not that fancy (ie. not monitoring battery life).Paleethnology
P
12

As per apple documentation "If the app moves into the background, the framework stops advertising and browsing and disconnects any open sessions. Upon returning to the foreground, the framework automatically resumes advertising and browsing, but the developer must reestablish any closed sessions" Refer: Apple doc

One way of extending the connection is as follows

Answering my own question, hoping it would help people in the same situation. For people new to iOS development, "using a background service" simple means turning on the "Background Modes" option in the "Capabilities" tab of your target. That alone should give your app around 10 minutes life in the background before it gets killed.

But, when the app goes to background, I use the "backgroundTimeRemaining" to know how much time I have left, it just starts at 180 (in sec, so 3 minutes), yet, the printing loop did continue to work passed three minutes, which means there is a need to manually code what should happen when the time is reached.

For Multipeer Connectivity, this is enough to maintain the connection alive when the app enters background, and it will still receive all messages/streams without a problem.

For the sake of stability, I do some cleaning as follow:

In the appDelegate.h

@property (nonatomic) UIBackgroundTaskIdentifier backgroundTask; //declaring a background task

In the appDelegate.m

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    self.backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^
                           {
                               //This is called 3 seconds before the time expires
                               //Here: Kill the session, advertisers, nil its delegates,
                               //      which should correctly send a disconnect signal to other peers
                               //      it's important if we want to be able to reconnect later,
                               //      as the MC framework is still buggy
                               [application endBackgroundTask:self.backgroundTask];
                               self.backgroundTask = UIBackgroundTaskInvalid; //Invalidate the background task
                           }];
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Here: We should init back the session, start the advertising and set the delegates from scratch
    // This should allow the app to reconnect to the same session with more often than not
    self.backgroundTask = UIBackgroundTaskInvalid; //Here we invalidate the background task if the timer didn't end already
}
Paleethnology answered 16/9, 2014 at 1:37 Comment(1)
It is not true and doesnt work. Once I put the app in background, my session stopped working immediately, not after 10 minutes, or even after minute... All my peers are disconnected.Benison
G
2

I've asked this same question once on the apple developer forums. One of the Apple employees told me that basically all of the Multipeer connectivity should be considered off-limits when your app is not int the foreground.

Girth answered 12/9, 2014 at 6:5 Comment(2)
Yes, the general undocumented rule is that MC shouldn't be working in the background ... I am still unsure if it's a "rule" (such as the app would be rejected from the App store if it does so), or just unsupported for other reasons (layer limitation, CPU usage, etc). For now, I use this for a corporate app, so it doesn't matter as long as it doesn't bug/crash.Paleethnology
@Paleethnology When you say you are using a corporate app, does that mean you've bypassed the time limit entirely? I.e. Are you able to keep MC running in the background for longer than 10 minutes?Nuzzle

© 2022 - 2024 — McMap. All rights reserved.