Maintain a Multipeer Connectivity session in Background via BackgroundTask?

房东的猫 提交于 2019-12-03 10:04:09

问题


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.


回答1:


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
}



回答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.



来源:https://stackoverflow.com/questions/25801665/maintain-a-multipeer-connectivity-session-in-background-via-backgroundtask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!