Online Book Reader

Home Category

iPhone Game Development - Chris Craft [97]

By Root 1657 0
the delegate in order to do anything with the session the peer picker configures for you. In order to do this, your class must implement GKPeerPickerControllerDelegate:

@protocol GKPeerPickerControllerDelegate

@optional

- (void)peerPickerController:(GKPeerPickerController *)picker

didSelectConnectionType:(GKPeerPickerConnectionType)type;

- (GKSession *)peerPickerController:(GKPeerPickerController *)picker

sessionForConnectionType:(GKPeerPickerConnectionType)type;

- (void)peerPickerController:(GKPeerPickerController *)picker

didConnectPeer:(NSString *)peerID toSession:(GKSession *)session;

- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker;

@end

Here are the details of how the GKPeerPickerControllerDelegate is implemented in P2PChat:

1. peerPickerController:didSelectConnectionType:

This event notifies the delegate that the user chose a connection type. Online connections are not implementing in this example; for this reason, the Peer Picker dialog box is now closed by calling dismiss. Next, a message is displayed to the user explaining why nothing happened. Finally, the Find button is re-enabled so the user can start the process all over again:

- (void)peerPickerController:(GKPeerPickerController*)

picker

didSelectConnectionType:(GKPeerPickerConnectionType)

type {

if (type == GKPeerPickerConnectionTypeOnline) {

[picker dismiss];

UIAlertView *alertView = [[UIAlertView alloc]

initWithTitle:@”Information”

message:@”Online connections are not supported.”

delegate:nil cancelButtonTitle:@”Close”

otherButtonTitles:nil];

[alertView show];

[alertView release];

findButton.enabled = true;

}

}

2. peerPickerController:sessionForConnectionType:

This event notifies the delegate that the connection type is requesting a GKSession object. You should return a valid GKSession object for use by the picker. If this method is not implemented or returns nil, a default GKSession will be created for you:

-(GKSession*)peerPickerController:

(GKPeerPickerController*)picker

sessionForConnectionType:(GKPeerPickerConnectionType)

type {

if(!gameSession) {

gameSession = [[GKSession alloc]

initWithSessionID:@”com.appsamuck.p2pchat”

displayName:nil sessionMode:GKSessionModePeer];

gameSession.delegate = self;

[gameSession setDataReceiveHandler:self withContext:nil];

}

return gameSession;

}

We will look at the creation of GKSession in more depth in the next section.

3. peerPickerController:didConnectPeer:toSession:

This event notifies the delegate that the peer was connected to a GKSession. Once this is fired, we know we can start communicating on our connection. In P2P Chat, we enable the Speak button when this event fires:

- (void)peerPickerController:(GKPeerPickerController*)

picker

didConnectToPeer:(NSString*)peerId {

speakButton.enabled = true;

}

4. peerPickerControllerDidCancel:

This event notifies the delegate that the user cancelled the picker. Use this event if you need to perform an action when the player cancels the connection process: You need to re-enable the Find button here also:

- (void)peerPickerControllerDidCancel:

(GKPeerPickerController*)picker {

findButton.enabled = true;

}

Introducing GKSession

Now that you have a peer picker set up, you can implement the GKSession object that you will use to send and receive messages between devices. In P2P Chat, the GKSession object is initialized in the peerPickerController:sessionForConnectionType: method implementation for the protocol GKPeerPickerControllerDelegate:

-(GKSession*)peerPickerController:(GKPeerPickerController*)picker

sessionForConnectionType:(GKPeerPickerConnectionType)type {

if(!gameSession) {

gameSession = [[GKSession alloc]

initWithSessionID:@”com.appsamuck.p2pchat”

displayName:nil sessionMode:GKSessionModePeer];

gameSession.delegate = self;

}

return gameSession;

}

This line creates and initializes the session:

gameSession = [[GKSession

Return Main Page Previous Page Next Page

®Online Book Reader