TLN Tutorial – Peer Picker Basics

TLN Tutorial – Peer Picker Basics

Read this before continuing to creating your own.

This tutorial will guide you through setting up a view controller so it will be ready for use with a peer picker. In reality, it is pretty simple. What you need to bear in mind though is that the peer picker itself is very temperamental sometimes. Also it takes around 30 seconds for your first connection to be made as the Bluetooth antenna uses the same antenna as the Wi-Fi one does in the iPhone. Hence, Bluetooth works best when the Wi-Fi connection is turned off.

I recommend having a device ready for this tutorial as there are huge difference between testing on simulator and a device, especially where the Bluetooth connection is concerned. In the simulator, the ‘Bluetooth connection is not done via the integrated mac Bluetooth but over the wi-fi network. It basically poses as ‘Bluetooth‘. So when making a multiplayer game, you reallyneed to test it on devices.

Ok , first up download this template that basically is just a view controller. All the frameworks that you need have been included, but i will run through them. PeerPickerTutorialBase

  • Lets start at the start. Open Xcode with this downloaded template. It is empty as you can see apart from a single view controller. Hit CMD+Enter to launch it in the Simulator. You s hould have an empty view with just the word FooBar on it.
  • Now, go to the view controller for this project, named PeerPickerTutorial View Controller. Rolls off the tongue doesn’t it. Within the implementation file (THE M FILE) de-comment out viewDidLoad. Maybe even put a log in there just so you know that it has loaded.

PeerPicker001

  • Right that is a nice easy part. Now i want you to import the GameKit.Framework into the frameworks folder. But don’t copy the files into your project. That is just bad news.
  • Add import code line to the top of your view controller .h file.
  • Also, make sure that the header file is the delegate of GKSession and PeerPicker Controller. If you don’t understand delegation I suggest you look further back into other tutorials. If you want to carry on though, the code looks like it does below.

PeerPicker003

  • In your View Controller h file, it is now time to set up the varialbes that the peer picker will be using. First you need a GKSession variable, call this anything you like, mine is called gameSession, for ease of remembering.
  • You will then need an NSString variable to hold your peers devices name in, call this opponentId.
  • You then need a heartbeat variable which is of type NSDate. This is taken from the apple documentation and this variable is used to determine packet loss etc.
  • Finally two integer variables, called gameUniqueID and gamePacketNumberPeerPicker004
  • Those are the variables that you need to get the peer picker up and running.
  • Set gameSession, opponentID and peerName is preoperties and don’t forget to synthesize them. This is important as these variables are important.
  • Now onto the m file. The apple GKTank source code can give you all the code for the next bit, but I recommend that you don’t just copy and paste it out and then raise your hands in celebration. It is better to understand what goes on with the PeerPicker. Read the apple documentation here.
  • First of all you need to be able to get the peer picker up and running. this involves a new function that can be called from say a button, or touching a screen. I prefer a button, so that is how we are going to do it.
  • In Interface Builder, drag a button onto the view. Type the title ‘Search’ on it. Now create an IBOutlet in the header so we can link to this button and call it searchButton. The function created is called startPicker and this function needs to be an IBAction and here is what you put inside of it.

PeerPicker005

  • NOTE: If you are trying to connect and it is not appearing in Interface Builder, make sure that the function is declared in the header. That is a common error. Right take a breather or something as you are going to need it. It now becomes code intense.
  • The peer picker will not work or be of any use until you implement its delegated functions.  These are as follows:
  1. -(GKSession*) peerPickerController: (GKPeerPickerController*) controller sessionForConnectionType: (GKPeerPickerConnectionType) type;
  2. - (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session;
  3. - (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker;
  4. - (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
  • Those four functions are delegate functions and respond to data sent back from the peer picker. So what you put in these functions determines the functionality of the picker. You can these functions available in any delegate, they do not have to be the view controller, that is if you wanted your peer picker to appear on a different view. Anyway i digress.
  • These functions are now full of amazing code as you can see from the final tutorial file, that is if you skipped ahead you naughty chickens. If you haven’t skipped ahead, enter the following into your functions:
PeerPicker006PeerPicker007PeerPicker008
PeerPicker009Those Pictures are the biggest i can get them without becoming too grainy. Click on them to get full size!!
  • Right with that code in your .m file, you should be ready to give it a go. The hostGame and joinGame are functions that I will come to in the next tutorial, so for now, just comment them out.
  • For now, you can test this in the simulator, run it and hit your button. You should see the following,
  • PeerPicker010Congratulations, your peer picker is active. Now if this code is installed on another mac with a simulator the two devices will be able to connect. Give it a go is all i say. Any comments to make or areas of confusion drop us a comment or an email.
  • Alas, here is the final code as well. PeerPickerTutorial

About the Author

Adam runs his own company creating iPhone games and web based applications. He also enjoys standing in the rain with no clothes on whilst humming fancy tunes.