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

Peer Pressure: Adventures in Multipeer Connectivity

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!

83231ffaec4a72ef96d26526872961ac?s=128

Jay Thrash

May 09, 2014
Tweet

Transcript

  1. Peer Pressure Adventures in Multipeer Connectivity Jay Thrash CocoaConf Raleigh

    May 2014
  2. Multipeer Connectivity Overview

  3. Multipeer Connectivity Overview What does it do?

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

    nearby iOS devices” Multipeer Connectivity Overview nearby - WWDC, June 2013
  5. Multipeer Connectivity Overview What does it do? No, really…

  6. “A framework for creating an ad-hoc network of nearby iOS

    devices” Multipeer Connectivity Overview - Me, just now
  7. Multipeer Connectivity Overview Wi-Fi Bluetooth Peer-to-Peer Wi-Fi R.Y.O.N.N. It’s Magic!

  8. “A framework for creating an ad-hoc network of that allows

    nearby iOS devices to communicate” Multipeer Connectivity Overview - Me, (again), just now
  9. Multipeer Connectivity Overview Why does it matter?

  10. ✉️ ✉️ ✉️ Multipeer Connectivity Overview ✉️

  11. ✉️ ✉️ ✉️ Multipeer Connectivity Overview ✉️ ☁

  12. ✉️ ✉️ ✉️ Multipeer Connectivity Overview

  13. Multipeer Connectivity Overview MOREEfficient Secure

  14. Multipeer Connectivity Lifecycle

  15. Multipeer Connectivity Lifecycle Advertise Discover Invite Connect Communicate

  16. Multipeer Connectivity Lifecycle Advertise Discover Invite Connect Communicate

  17. Multipeer Connectivity Lifecycle Advertise Discover Invite Connect Communicate ? ?

  18. Multipeer Connectivity Lifecycle Advertise Discover Invite Connect Communicate ✅ ✅

  19. Multipeer Connectivity Lifecycle Advertise Discover Invite Connect Communicate

  20. Multipeer Connectivity Lifecycle Advertise Discover Invite Connect Communicate

  21. Multipeer Connectivity Implement

  22. Multipeer Connectivity Implement @import MultipeerConnectivity; Step 0: Import Framework

  23. Multipeer Connectivity Implement MCPeerID peerID = [[MCPeerID alloc] initWithDisplayName:peerName]; Step

    1: Create Identity
  24. Multipeer Connectivity Implement MCSession session = [[MCSession alloc] initWithPeer:peerID]; Step

    2: Create Session
  25. Multipeer Connectivity Implement MCAdvertiserAssistant advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:serviceType discoveryInfo:nil

    session:session]; ! advertiser.delegate = self; [advertiser start]; Step 3: Advertise
  26. 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
  27. Multipeer Connectivity Implement MCSession Step 5: Communicate Message Stream Resources

  28. 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
  29. Multipeer Connectivity Implement MCSessionDelegate // Receive Message -(void) session:(MCSession *)session

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

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

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

    didStartReceivingResourceWithName:(NSString *) resourceName:(NSString *) fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *) prg ! Step 5: Communicate
  34. 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
  35. Multipeer Connectivity Implement MCSession ! // Leave Session [session disconnect];

    Step 5: Connection Status
  36. Multipeer Connectivity Implement MCSessionDelegate // Someone left the session !

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

    thread ! dispatch_async(dispatch_get_main_queue(), ^{ [self doThingWithData]; }); Step 5: Communicate !
  38. Multipeer Connectivity D V M emo inimum iable

  39. Multipeer Connectivity Demo

  40. Multipeer Connectivity Demo Bob Mary Bob Mary Jay

  41. Multipeer Connectivity Demo Bob Mary Bob Mary Jay

  42. Multipeer Connectivity Demo Bob Mary Jay

  43. Multipeer Connectivity Demo Bob Mary Jay

  44. Multipeer Connectivity Demo Bob Mary Jay

  45. Multipeer Connectivity Demo

  46. Extra Credit Some things we didn’t cover •Programmatic advertising, discovery

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

    •Objc.io (drones!!) •Apple Docs (MP Framework)
  48. Thanks! @jaythrash jay@jaythrash.com http://jaythrash.com/talks