Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Adventures in Multipeer Connectivity

Jay Thrash
October 07, 2014

Adventures in Multipeer Connectivity

Originally presented in Greenville, SC at 360|iDev min on 2014-10-07.

The Multipeer Connectivity framework included in iOS 7 represents a fundamental shift in how networked apps can be built. This woefully underused framework allows developers to create efficient and secure peer-to-peer networks that leverage the Wi-Fi and Bluetooth personal area network capabilities contained in all modern iOS devices. However, as complicated as this all might sound, don’t worry. The dirty details of all this infrastructure networking have been wrapped up within an API that’s relatively compact, easy to understand, and quite fun to use!

Jay Thrash

October 07, 2014
Tweet

More Decks by Jay Thrash

Other Decks in Technology

Transcript

  1. “A framework for discovering services provided by, and communicating with,

    nearby iOS devices” Multipeer Connectivity Overview - WWDC, June 2013
  2. “A framework for discovering services provided by, and communicating with,

    nearby iOS and OS X devices” Multipeer Connectivity Overview - WWDC, June 2014
  3. “A framework for creating an ad-hoc network of nearby iOS

    & OS X devices” Multipeer Connectivity Overview - Me, just now
  4. “A framework for creating an ad-hoc network of that allows

    nearby iOS & OS X devices to communicate” Multipeer Connectivity Overview - Me, (again), just now
  5. Multipeer Connectivity Implement MCBrowserViewController browser = [[MCBrowserViewController alloc] initWithServiceType:serviceType session:session];

    browser.minimumNumberOfPeers = min; browser.maximumNumberOfPeers = max; browser.delegate = self; [self presentViewController:browser ...]; Step 4: Discover
  6. Multipeer Connectivity Implement MCSession // Send Message [session sendData:messageData toPeers:@[peerID,peerID,peerID,…]

    withMode:MCSessionSendDataReliable error:&error]; // MCSessionSendDataReliable is faster, but… // delivery is not guaranteed // nor is order Step 5: Communicate
  7. Multipeer Connectivity Implement MCSessionDelegate // Receive Message -(void) session:(MCSession *)session

    didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID { // Unarchive data to object } Step 5: Communicate
  8. Multipeer Connectivity Implement MCSession // Create Stream NSOutputStream *stream =

    [session startStreamWithName:streamName toPeer:peerID error:&error] // write to stream Step 5: Communicate
  9. Multipeer Connectivity Implement MCSessionDelegate // Receive on stream - (void)session:(MCSession

    *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)name fromPeer:(MCPeerID *)peerID Step 5: Communicate
  10. Multipeer Connectivity Implement MCSession // Send resource NSProgress *progress =

    [session sendResourceAtURL:url withName:name toPeer:peerID completionHandler:completionHandler]; // controlling transfer progress.fractionCompleted; [progress cancel]; Step 5: Communicate
  11. Multipeer Connectivity Implement MCSessionDelegate // Receiving Resource - (void)session:(MCSession *)session

    didStartReceivingResourceWithName:(NSString *) resourceName:(NSString *) fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *) prg Step 5: Communicate
  12. Multipeer Connectivity Implement MCSessionDelegate // Received Resource (void)session:(MCSession *) sessiondidFinishReceivingResourceWithName:(NSString*)

    resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error { // completed transfer } Step 5: Communicate
  13. Multipeer Connectivity Implement MCSessionDelegate // Someone left the session -(void)

    session:(MCSession *) peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state; { // MCSessionStateNotConnected // MCSessionStateConnecting // MCSessionStateConnected } Step 5: Connection Status
  14. Multipeer Connectivity Implement MCSession // handle changes on the main

    thread dispatch_async(dispatch_get_main_queue(), ^{ [self doThingWithData]; }); Step 5: Communicate !
  15. Extra Credit Some things we didn’t cover •Programmatic advertising, discovery

    and session joining (app specific) •Streaming data •Sending resources !
  16. Higher Learning Topics to Google •NSHipster •WWDC 2013 (Talk 708)

    •WWDC 2014 (Talk 709) •objc.io #8 Quadcopter Project •Apple Docs (MP Framework)