Multipeer Connectivity

5,703 views

Published on

Come check out Multi-Peer Connectivity, a brand new framework for connecting multiple devices in a peer-to-peer network. We'll learn how to get a basic application up and running and take advantage of these new APIs to create a whole new interactive dynamic in your apps.

Published in: Technology

Multipeer Connectivity

  1. 1. Multi-Peer Connectivity
  2. 2. Architecture
  3. 3. Multi-Peer Connectivity • New in iOS 7 • Ability to connect to a mesh of peers. • Able to connect to peers over WiFi, ad-hoc wireless, and Bluetooth. • Peers must be ‘nearby’.
  4. 4. Terms • Advertiser - Broadcasts availability to connect • Browser - Invites advertisers to join a session
  5. 5. MCSession • MCSession manages all communication between peers. • Must be used by all participants.
  6. 6. Create A Session self.peerID = [[MCPeerID alloc] initWithDisplayName:@"Jill"]; ! self.currentSession = [[MCSession alloc] initWithPeer:self.peerID]; self.currentSession.delegate = self;
  7. 7. Discovery Browser Advertiser Advertiser Who wants to chat? Ready to chat! Ready to chat!
  8. 8. Advertising self.peerID = [[MCPeerID alloc] initWithDisplayName:@"Jill"]; ! self.advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:self.peerID discoveryInfo:nil serviceType:@"whartman-chat"]; self.advertiser.delegate = self; [self.advertiser startAdvertisingPeer];
  9. 9. Browsing self.peerID = [[MCPeerID alloc] initWithDisplayName:@"Jack"]; ! self.browser = [[MCNearbyServiceBrowser alloc] initWithPeer:self.peerID serviceType:@"whartman-chat"]; self.browser.delegate = self; [self.browser startBrowsingForPeers];
  10. 10. Browser: Discover & Invite - (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info { [browser invitePeer:peerID toSession:self.currentSession withContext:nil timeout:3.0]; }
  11. 11. Advertiser: Accept Invitation - (void)advertiser:(MCNearbyServiceAdvertiser *)advertiser didReceiveInvitationFromPeer: (MCPeerID *)peerID withContext:(NSData *)context invitationHandler:(void(^)(BOOL accept, MCSession *session))invitationHandler { // In most cases you will want to give users an option to connect or not. invitationHandler(YES, self.currentSession); [self.advertiser stopAdvertisingPeer]; // Once invited, stop advertising }
  12. 12. Sending Data To Peers NSDictionary *message = @{ @"message" : @"Hello!" }; NSData *messageData = [NSJSONSerialization dataWithJSONObject:message options:0 error:nil]; ! NSError *error = nil; ! [self.currentSession sendData:messageData toPeers:self.currentSession.connectedPeers withMode:MCSessionSendDataReliable error:&error];
  13. 13. Receiving Data From Peers - (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID { NSError *error = nil; ! NSDictionary *recievedData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; ! if (!recievedData) { NSLog(@"error decoding message! %@", error); } else { // Display message to the user... } }
  14. 14. Sending Resources To Peers NSString *filePath = [[NSBundle mainBundle] pathForResource:@"hello" ofType:@"jpg"]; NSURL *imageURL = [NSURL fileURLWithPath:filePath]; ! [self.currentSession sendResourceAtURL:imageURL withName:@"hello.jpg" toPeer:peer withCompletionHandler:^(NSError *error) { if (error) { NSLog(@"Error sending image! %@", error); } }];
  15. 15. Receiving Resources From Peers - (void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress { NSLog(@"downloading file: %f%%", progress.fractionCompleted); } ! ! ! ! - (void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error { ! if (error) { NSLog(@"Error when receiving file! %@", error); } else { // Present the resource to the user. } }
  16. 16. Demo
  17. 17. Best Practices • Keep messages short. • Be careful with inviting/accept invitations. • As an advertiser, once you have accepted an invitation, stop advertising.
  18. 18. Limitations • Limit to 8 connected peers. • Data transfer is slow—especially on Bluetooth.
  19. 19. Questions?
  20. 20. Resources • Nearby Networking with Multipeer Connectivity (WWDC 2013) • Demo App - https://github.com/waynehartman/MultiPeerTest/

×