iOS Reference Library Apple Developer
Search

Finding Peers with Peer Picker

The peer picker provides a standard user interface for connecting two users via Bluetooth. Optionally, your application can configure the peer picker to allow a user to choose between an Internet and Bluetooth connection. If an Internet connection is chosen, your application must dismiss the peer picker dialog and present its own user interface to complete the connection.

After you’ve read this article, you should read “Working with Sessions” to see what your application can do with the created session.

To add a peer picker to your application, create a new class to hold the peer picker controller’s delegate methods. Follow these steps:

  1. Create and initialize a GKPeerPickerController object.

    picker = [[GKPeerPickerController alloc] init];
  2. Attach the delegate (you’ll define its methods as you proceed through these steps).

    picker.delegate = self;
  3. Configure the allowed network types.

    picker.connectionTypesMask = GKPeerPickerConnectionTypeNearby | GKPeerPickerConnectionTypeOnline;

    Normally, the peer picker defaults to Bluetooth connections only. Your application may also add Internet (online) connections to the connection types mask. If your application does this, it must also implement the peerPickerController:didSelectConnectionType: method.

  4. Optionally, implement the peerPickerController:didSelectConnectionType: method to dismiss the dialog when an Internet connection is selected.

    - (void)peerPickerController:(GKPeerPickerController *)picker didSelectConnectionType:(GKPeerPickerConnectionType)type {
        if (type == GKPeerPickerConnectionTypeOnline) {
            picker.delegate = nil;
            [picker dismiss];
            [picker autorelease];
           // Implement your own internet user interface here.
        }
    }
  5. Implement the delegate’s peerPickerController:sessionForConnectionType: method.

    - (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type
    {
        GKSession* session = [[GKSession alloc] initWithSessionID:myExampleSessionID displayName:myName sessionMode:GKSessionModePeer];
        [session autorelease];
        return session;
    }

    Your application needs to implement this only if it wants to override the standard behavior of the peer picker controller.

  6. Implement the delegate’s peerPickerController:didConnectPeer:toSession: method to take ownership of the configured session.

    - (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession: (GKSession *) session {
    // Use a retaining property to take ownership of the session.
        self.gameSession = session;
    // Assumes our object will also become the session's delegate.
        session.delegate = self;
        [session setDataReceiveHandler: self withContext:nil];
    // Remove the picker.
        picker.delegate = nil;
        [picker dismiss];
        [picker autorelease];
    // Start your game.
    }
  7. Your application also needs to implement the peerPickerControllerDidCancel: method to react when the user cancels the picker.

    - (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
    {
        picker.delegate = nil;
        // The controller dismisses the dialog automatically.
        [picker autorelease];
    }
  8. Add code to show the dialog in your application.

    [picker show];



Last updated: 2009-05-28

Did this document help you? Yes It's good, but... Not helpful...