In the GameKit world, “online” currently
means any valid connection style other than Bluetooth. You might use a
local WLAN network to connect to another device on the same network or
connect through WWAN (i.e., the cellular service) or WiFi to a remote
Internet-based host. GameKit takes you only so far as the dialog shown
in Figure 1. By selecting Online, your user depends on you to create a custom connection to another device or service.
You create this two-item dialog by supplying the
online option to the peer picker mask. In all other ways, there’s no
change in how you create and present a standard GameKit peer picker
controller.
- (void) startConnection
{
if (!self.isConnected)
{
GKPeerPickerController *picker = [[GKPeerPickerController
alloc] init];
picker.delegate = self;
picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby |
GKPeerPickerConnectionTypeOnline;
[picker show];
if (self.viewController)
self.viewController.navigationItem.rightBarButtonItem =
nil;
}
}
Catch the user selection in the peerPickerController:didSelectConnectionType:
callback. You can assume that if the user selected Nearby that all the
handshaking dialogs are taken care of for you. Should the user select
Online, however, it’s up to you to move things to the next step. You
need to dismiss the picker and display the next stage of the connection
task. Here, control passes away from the peer picker.
- (void)peerPickerController:(GKPeerPickerController *)picker
didSelectConnectionType:(GKPeerPickerConnectionType)type
{
if(type == GKPeerPickerConnectionTypeOnline)
{
[picker dismiss];
[picker release];
[BonjourHelper sharedInstance].sessionID = self.sessionID;
[BonjourHelper sharedInstance].viewController =
self.viewController;
[BonjourHelper sharedInstance].dataDelegate =
self.dataDelegate;
[BonjourHelper connect];
}
}
As Recipe 1 demonstrates, almost no changes are needed from the BonjourHelper side of things. The Connect button on the navigation bar must point back to GameKit’s connect method, not to BonjourHelper’s.
This ensures that users can finish a Bonjour connection and then move
on to a Bluetooth one without restarting the application.
Recipe 1. Updating the Macro Code to Use GameKit’s Version of Connect
#define GBARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] \
initWithTitle:TITLE style:UIBarButtonItemStylePlain \
target:[GameKitHelper class] action:SELECTOR] autorelease]
if (sharedInstance.viewController)
sharedInstance.viewController.navigationItem.rightBarButtonItem =
GBARBUTTON(@"Connect", @selector(connect));
|
Additionally, you might think the Info.plist file needs a UIRequiresPersistentWiFi
key set to the Boolean value of true. Avoid doing this. Instead, check
for WiFi only when you are ready to attempt to create a WiFi connection,
i.e., when the user clicks Online. GameKit Bluetooth connections don’t
need persistent WiFi although standard Bonjour ones do. Don’t require
your users to connect to a (possibly nonexistent) WiFi service when
Bluetooth is sufficient for Nearby gaming.