Multipeer connection with one device running in the background

I would like to connect 2 devices using a gateway connection infrastructure where one of these devices runs an app in the background, just like Firechat (I cannot confirm that it works, I installed it on iPhone 5S and 4, but they just cannot find each other, but I read somewhere that it works).

What's the best way to achieve this?

I am using the following two methods from the example code:

-(void)setupPeerAndSessionWithDisplayName:(NSString *)displayName{
    _peerID = [[MCPeerID alloc] initWithDisplayName:displayName];

    _session = [[MCSession alloc] initWithPeer:_peerID];
    _session.delegate = self;
}


-(void)setupMCBrowser{
    _browser = [[MCBrowserViewController alloc] initWithServiceType:@"chat-files" session:_session];
}


-(void)advertiseSelf:(BOOL)shouldAdvertise{
    if (shouldAdvertise) {
        _advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"chat-files"
                                                           discoveryInfo:nil
                                                                 session:_session];
        [_advertiser start];
    }
    else{
        [_advertiser stop];
        _advertiser = nil;
    }
}

      

When I run the app in the foreground, it finds the other device flawlessly and connects as well. But if I put one of the apps in the background, the background app-app is no longer visible.

I've already read the following: Can I start a tiered session in the background? - But I cannot believe that this is impossible without some workarounds.

FYI, my app also uses background location updates, if relevant ...

Thank!

EDIT: Is there any other way to do this? Basically I just want to send a message to another device (whoever is running in the background). Since the Multipeer connection is not running in the background, can I connect directly via bluetooth?

+2


source to share


1 answer


Apple has confirmed that "Multipeer Connectivity is not running in the background." The reason is that if your application is suspended, its socket resources can be restored and everything will fall apart.

However, iBeacons can be tracked in the background. Basically, you've configured an app to monitor the proximity of a beacon with a specific identifier. Then your foreground app will become a beacon with the same ID. This results in the following application delegate method being called in the application in the background:



- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region {
    UILocalNotification *notification = [[UILocalNotification alloc] init];

    if(state == CLRegionStateInside) {
        notification.alertBody = NSLocalizedString(@"A nearby app would like to connect", @"");
    } else {
        return;
    }
    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}

      

This is where you post a local notification that when you click on it, the app will bring it to the front, after which you can start advertising for a peer-to-peer connection.

+4


source







All Articles