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

Peer Pressure: Adventures in Multipeer Connecti...

Peer Pressure: Adventures in Multipeer Connectivity

This presentation on Multipeer Connectivity was originally given at CocoaConf Raleigh, May 2014.

The Multipeer Connectivity framework included in iOS 7 represents a fundamental shift in how networked apps can be built. Using the Multipeer framework allows developers to tap into a wide range of nearby networking solutions like peer-to-peer Wi-Fi, infrastructure Wi-Fi networks and Bluetooth personal area networks. 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

May 09, 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 nearby - WWDC, June 2013
  2. “A framework for creating an ad-hoc network of nearby iOS

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

    nearby iOS devices to communicate” Multipeer Connectivity Overview - Me, (again), just now
  4. 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
  5. 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
  6. Multipeer Connectivity Implement MCSessionDelegate // Receive Message -(void) session:(MCSession *)session

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

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

    *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)name fromPeer:(MCPeerID *)peerID ! Step 5: Communicate
  9. 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
  10. Multipeer Connectivity Implement MCSessionDelegate // Receiving Resource - (void)session:(MCSession *)session

    didStartReceivingResourceWithName:(NSString *) resourceName:(NSString *) fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *) prg ! Step 5: Communicate
  11. 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
  12. Multipeer Connectivity Implement MCSessionDelegate // Someone left the session !

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

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

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

    •Objc.io (drones!!) •Apple Docs (MP Framework)