Maintain a Multipeer session in the background using BackgroundTask?

I am trying to maintain a MultipeerConnectivity session when the application temporarily enters the background, so I thought about using a background task, as I saw several times here ... The problem is that I have no idea how to “support” the session using UIBackgroundTask. can someone please write a hint

I do not need advertisers / browsers, everything is in order to stop them, but I would like the session to not disconnect, since reconnecting is currently super buggy.

+4
source share
2 answers

" , . , " Refer: Apple doc

, , . , iOS, " " " " "" . 10 , .

, "backgroundTimeRemaining", , , 180 ( , 3 ), , , , , .

Multipeer , , , / .

:

appDelegate.h

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

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 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
}
+10

Apple. Apple , Multipeer , .

0

All Articles