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

Adventures in Multipeer Connectivity

83231ffaec4a72ef96d26526872961ac?s=47 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!

83231ffaec4a72ef96d26526872961ac?s=128

Jay Thrash

October 07, 2014
Tweet

Transcript

  1. in Multipeer Connectivity Jay Thrash [360|iDev min] October 2014 Adventures

    Adventures
  2. Overview Lifecycle Implement Demo

  3. Multipeer Connectivity Overview

  4. Multipeer Connectivity Overview What does it do?

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

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

    nearby iOS and OS X devices” Multipeer Connectivity Overview - WWDC, June 2014
  7. Multipeer Connectivity Overview What does it do? No, really…

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

    & OS X devices” Multipeer Connectivity Overview - Me, just now
  9. Multipeer Connectivity Overview Wi-Fi Bluetooth Peer-to-Peer Wi-Fi R.Y.O.N.N.

  10. Multipeer Connectivity Overview Wi-Fi Bluetooth Peer-to-Peer Wi-Fi R.Y.O.N.N. It’s Magic!

  11. “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
  12. Multipeer Connectivity Overview Why does it matter?

  13. ✉️ ✉️ ✉️ Multipeer Connectivity Overview ✉️

  14. ✉️ ✉️ ✉️ Multipeer Connectivity Overview ✉️ ☁

  15. ✉️ ✉️ ✉️ Multipeer Connectivity Overview ✉️

  16. ✉️ ✉️ ✉️ Multipeer Connectivity Overview

  17. ✉️ ✉️ ✉️ Multipeer Connectivity Overview

  18. Multipeer Connectivity Overview MOREEfficient Secure

  19. Multipeer Connectivity Overview

  20. Multipeer Connectivity Lifecycle

  21. Multipeer Connectivity Lifecycle Advertise Discover Invite Connect Communicate

  22. Multipeer Connectivity Lifecycle Advertise Discover Invite Connect Communicate

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

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

  25. Multipeer Connectivity Lifecycle Advertise Discover Invite Connect Communicate

  26. Multipeer Connectivity Lifecycle Advertise Discover Invite Connect Communicate

  27. Multipeer Connectivity Implement

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

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

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

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

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

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

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

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

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

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

    5: Connection Status
  42. Multipeer Connectivity Implement MCSessionDelegate // Someone left the session -(void)

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

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

  45. D V M emo inimum iable Multipeer Connectivity

  46. Multipeer Connectivity Demo

  47. Multipeer Connectivity Demo Bob Mary Bob Mary Jay

  48. Multipeer Connectivity Demo Bob Mary Bob Mary Jay

  49. Multipeer Connectivity Demo Bob Mary Jay

  50. Multipeer Connectivity Demo Bob Mary Jay

  51. Multipeer Connectivity Demo Bob Mary Jay

  52. Multipeer Connectivity Demo Bob Mary Jay

  53. Multipeer Connectivity Demo

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

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

    •WWDC 2014 (Talk 709) •objc.io #8 Quadcopter Project •Apple Docs (MP Framework)
  56. @jaythrash jay@jaythrash.com http://jaythrash.com/talks

  57. Thanks! @jaythrash jay@jaythrash.com http://jaythrash.com/talks