Upload
waynehartman
View
3.382
Download
1
Embed Size (px)
DESCRIPTION
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.
Citation preview
Multi-Peer Connectivity
Architecture
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’.
Terms
• Advertiser - Broadcasts availability to connect
• Browser - Invites advertisers to join a session
MCSession
• MCSession manages all communication between peers.
• Must be used by all participants.
Create A Session
self.peerID = [[MCPeerID alloc] initWithDisplayName:@"Jill"]; !self.currentSession = [[MCSession alloc] initWithPeer:self.peerID]; self.currentSession.delegate = self;
Advertiser AdvertiserBrowser
Ready to chat!
Ready to chat!
Who wants to chat?
Discovery
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];
Browsing
self.peerID = [[MCPeerID alloc] initWithDisplayName:@"Jack"]; !self.browser = [[MCNearbyServiceBrowser alloc] initWithPeer:self.peerID serviceType:@"whartman-chat"]; self.browser.delegate = self; [self.browser startBrowsingForPeers];
Browser: Discover & Invite
- (void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(MCPeerID *)peerID withDiscoveryInfo:(NSDictionary *)info { [browser invitePeer:peerID toSession:self.currentSession withContext:nil timeout:3.0]; }
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 }
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];
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... } }
Sending Resources To PeersNSString *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); } }];
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. } }
Demo
Best Practices
• Keep messages short.
• Be careful with inviting/accept invitations.
• As an advertiser, once you have accepted an invitation, stop advertising.
Limitations
• Limit to 8 connected peers.
• Data transfer is slow—especially on Bluetooth.
Questions?
Resources
• Nearby Networking with Multipeer Connectivity (WWDC 2013)
• Demo App - https://github.com/waynehartman/MultiPeerTest/