2

I'm currently making a 2-player strategy board game in Swift and need to connect two iPads over local WiFi or Bluetooth. No matter what I've tried today, I can't get them to detect each other (I've tried over local WiFi and Bluetooth).

Here is my authorization code which runs in the UIViewController when my app first launches (which always returns "Self local player is authenticated." along with the ID:

private func authenticateLocalPlayer() {
    var localPlayer = getLocalPlayer()

    // If Apple were doing their job right, this is what the proper code should look like:
    // var localPlayer = GKLocalPlayer.localPlayer()

    if ( !localPlayer.authenticated ) {
        localPlayer.authenticateHandler = { (viewController : UIViewController!, error : NSError!) -> Void in
            NSLog("Error: \(error)")
            if viewController != nil {

                // Authenticated?
                self.presentViewController(viewController, animated: true, completion: nil)
                NSLog("viewController is not nil")
            } else if (localPlayer.authenticated == true) {
                NSLog("Self local player is authenticated.")
                NSLog("My name is \(localPlayer.playerID)")
            } else {
                NSLog("Not authenticated")
                NSLog("Player is \(localPlayer.playerID)")
            }
        }
    } else {
        NSLog("Player is already authenticated!")
    }
}

and here is my code to detect nearby devices in a separate UIViewController:

override func viewDidLoad() {
    devicesLabel.text = "Waiting for devices..."
    searchForDevices()
    NSLog("Ran searchForDevices()")
}

private func searchForDevices() {
    GKMatchmaker.sharedMatchmaker().startBrowsingForNearbyPlayersWithHandler() {
        var status = $1 ? "true" : "false"
        self.devicesLabel.text = "Reachability changed for player \($0) with status: \(status)"
    }
}

No matter what I do with my two iPads (both are model iPad 3), neither one ever sees the other. Am I calling startBrowsingForNearbyPlayersWithHandler correctly?

Also notice that in the authorization code above, I'm using the Objective-C workaround recommended by this post: Game Center not authenticating using Swift, since the "Swift way" of doing that didn't work for me either.

I also ran Spelltower across both devices over local WiFi, so it looks like the hardware is functioning properly. Any idea what could be going wrong here?

Community
  • 1
  • 1
Chuck Smith
  • 2,101
  • 3
  • 16
  • 21
  • Can you make sure `GKMatchmaker.sharedMatchmaker()` is returning a shared matchmaker instance? Why should this work when `GKLocalPlayer.localPlayer()` isn't. – stigi Aug 05 '14 at 16:07
  • It returns from an NSLog as , so I would assume it is. Is there any better way to check that? From what I understand, Apple made GKLocalPlayer.localPlayer() a public method to Obj-C and a private method to Swift and we're all very confused as to why. – Chuck Smith Aug 05 '14 at 19:16
  • May I ask what version of iOS you were having this issue with? I had this working fine previously but with the latest (8.4) I've noticed this is also no longer working for me. – Shaun Budhram Aug 18 '15 at 05:34
  • I've filed a radar for this. If you decide to try it again, please let us know if you get it working. – Shaun Budhram Aug 18 '15 at 08:17

2 Answers2

0

I decided to abandon developing this through Game Center and to use Multipeer Connectivity instead.

Chuck Smith
  • 2,101
  • 3
  • 16
  • 21
0

You are not registering a class to receive invitation updates. You need to register a class and implement methods conforming to the protocol for GKLocalPlayerListener. See my response in this post (it is in Objective-C, but the same concept applies):

Some startBrowsingForNearbyPlayersWithReachableHandler questions

Community
  • 1
  • 1
Shaun Budhram
  • 3,690
  • 4
  • 30
  • 41