Presentation is loading. Please wait.

Presentation is loading. Please wait.

EEC-492/693/793 iPhone Application Development

Similar presentations


Presentation on theme: "EEC-492/693/793 iPhone Application Development"— Presentation transcript:

1 EEC-492/693/793 iPhone Application Development
Lecture 15 Wenbing Zhao & Nigamanth Sridhar 2/16/2019 EEC492/693/793 - iPhone Application Development

2 Outline Peer-to-Peer Communication with GameKit Demo Assignment:
GKPeerPickerController & GKPeerPickerControllerDelegate GKSession & GKSessionDelegate Objects  NSData Demo Assignment: Build the chat app using the GameKit Challenge: enable 3 or more peers to chat together Documentation on GameKit: 2/16/2019 2/16/2019 EEC492/693/793 - iPhone Application Development EEC492/693/793 - iPhone Application Development 2

3 EEC492/693/793 - iPhone Application Development
Game Kit Introduced in iOS 3.0 to enable the development of social games Game Kit consists of three technologies: Game center: centralized network service (supported in iOS 4.1 and later) Peer-to-peer networking (supported in iOS 3.0 and later) In-game voice chat (supported in iOS 3.0 and later) 2/16/2019 EEC492/693/793 - iPhone Application Development

4 Peer-to-Peer Connectivity
Abstracts away Bonjour and Stream creation Nearby (Bluetooth) & Online (Wifi) support 2/16/2019 EEC492/693/793 - iPhone Application Development

5 Peer-to-Peer Connectivity
Peer discovery: GKPeerPickerController The GKPeerPickerController presents the UI and responds to the user’s actions Resulting in a fully configured GKSession that connects two peers Peer communication: GKSession 2/16/2019 EEC492/693/793 - iPhone Application Development

6 GKPeerPickerController
Provides UI to connect to peer Allows user to pick between “Nearby” and “Online” Nearby Bluetooth communication Most of the work is handled for you under-the-hood Online Hands off responsibility to the application App builds or connects to server Associates users in server App is responsible for handling communication 2/16/2019 EEC492/693/793 - iPhone Application Development

7 Initiating a Connection
// Allocate the PeerPickerController GKPeerPickerController *peerPicker; peerPicker = [[GKPeerPickerController alloc] init]; // Set up delegate to receive callbacks peerPicker.delegate = self; peerPicker.connectionMask = GKPeerPickerConnectionTypeOnline | GKPeerPickerConnectionTypeNearby; // Display the Peer Picker [peerPicker show]; 2/16/2019 EEC492/693/793 - iPhone Application Development

8 Establishing a Connection
Return a session for the requested type (by the GKPeerPickerControllerDelegate) (called when you chose “Nearby”) - (GKSession *)peerPickerController: (GKPeerPickerController *)picker sessionForConnectionType: (GKPeerPickerConnectionType)type { GKSession *session = [[GKSession alloc] initWithSessionID:nil displayName:localName sessionMode:GKSessionModePeer]; return [session autorelease]; } Accept connection request from the remote peer (by the GKSession delegate) - (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID { [session acceptConnectionFromPeer:peerID error:nil]; 2/16/2019 EEC492/693/793 - iPhone Application Development

9 Establishing a Connection
Notification to the requesting peer on the establishment of the connection - (void) peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session { session.delegate = self; [session setDataReceiveHandler:self withContext:nil]; self.gameSession = session; // Done with the Peer Picker so dismiss it [picker dismiss]; [picker autorelease]; } 2/16/2019 EEC492/693/793 - iPhone Application Development

10 Other GKPeerPickerControllerDelegate Methods
- (void) peerPickerController:(GKPeerPickerController *)picker didSelectConnectionType:(GKPeerPickerConnectionType)type Should dismiss picker if the type is GKPeerPickerConnectionTypeOnline, and provide your own UI - (void) peerPickerControllerDidCancel:(GKPeerPickerController *)picker Do some cleaning up, if necessary 2/16/2019 EEC492/693/793 - iPhone Application Development

11 EEC492/693/793 - iPhone Application Development
GKSession Creating a GKSession GKSession *session = [[GKSession alloc] initWithSessionID:… - (id)initWithSessionID:(NSString *)sessionID displayName:(NSString *)name sessionMode:(GKSessionMode)mode typedef enum { GKSessionModeServer, GKSessionModeClient, GKSessionModePeer } GKSessionMode; 2/16/2019 EEC492/693/793 - iPhone Application Development

12 EEC492/693/793 - iPhone Application Development
GKSession Properties include: displayName - Your name, as seen by your peers peerID - unique ID, identifying your Session to your peers sessionID - String used by your Application to filter peers available – determines whether or not the session wants to connect to new peers Delegate methods: Notifies of state change of a peer’s session Notifies of connection requests Notifies of errors with sessions or connections 2/16/2019 EEC492/693/793 - iPhone Application Development

13 GKSession - Sending & Receiving Data
Send data to specific peers - (BOOL) sendData:(NSData *)data toPeers:(NSArray *)peers withDataMode:(GKSendDataMode)mode error:(NSError **)error; Data transmission mode: GKSendDataReliable, GKSendDataUnreliable Send data to ALL connected peers - (BOOL) sendDataToAllPeers:(NSData *)data withDataMode: (GKSendDataMode)mode error:(NSError **)error; Data handler method to receive data: - (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context; 2/16/2019 EEC492/693/793 - iPhone Application Development

14 How to Pack Data into NSData
From NSString to NSData NSData *data = [msgToSend.text dataUsingEncoding:NSUTF8StringEncoding]; From NSData to NSString NSString *recvStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; For more complicated data, can use Archiver // Objects to NSData NSMutableArray *touchesForUser = …; NSData *data = [NSKeyedArchiver archivedDataWithRootObject:touchesForUser]; // NSData to objects NSMutableArray *touchValues = [[NSKeyedUnarchiver unarchiveObjectWithData:data] mutableCopy]; 2/16/2019 EEC492/693/793 - iPhone Application Development


Download ppt "EEC-492/693/793 iPhone Application Development"

Similar presentations


Ads by Google