Question

I'd like to use the Multipeer Connectivity functionality for my app. Brief intro to the functionality of the app: The app should scan for other devices running the app (in background), connect to them and transfer a bit of data. All without interaction with the user.

Question: is it possible to connect to other devices using multipeer but without having to show the alert view that another device wants to connect and forcing the user to accept or decline the connection? Is there a way who I can programmatically accept all incoming connections from other devices? If so, how?

Thanks a lot in advance!

Was it helpful?

Solution

You have two questions here:

The app should scan for other devices running the app (in background)

The answer here is NO - MPC does not work in the background (see this so response)

For your second question:

is it possible to connect to other devices using multipeer but without having to show the alert

The answer is Yes .. all the detail you need can be found in the apple docs. Here's some snippets on what I do:

On one device - start the browser

_serviceBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:_peerID
                                                       serviceType:_sessionName];

[_serviceBrowser startBrowsingForPeers];

On the other device - start the advertiser

_serviceAdvertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:_peerID
                                                           discoveryInfo:nil
                                                             serviceType:_sessionName];
[_serviceAdvertiser startAdvertisingPeer];

These services implement delegate functions to advise your app of a possible connection. Here the browser is advised of an advertiser and now invites the peer to join in a session

- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID     withDiscoveryInfo:(NSDictionary *)info
{
    [browser invitePeer:peerID toSession:_session withContext:nil timeout:30.0];

}

The advertiser then responds

- (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer:    (MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler
{
    invitationHandler(YES, _session);
}

Now you will receive a session delegate call to advise you of the state of your peer connectivity. At this point you should be connected - no "real" user interaction required.

- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top