【问题标题】:startBrowsingForNearbyPlayersWithHandler() in iOS 8 in SwiftSwift 中 iOS 8 中的 startBrowsingForNearbyPlayersWithHandler()
【发布时间】:2014-08-05 15:42:14
【问题描述】:

我目前正在用 Swift 制作一个 2 人战略棋盘游戏,需要通过本地 WiFi 或蓝牙连接两台 iPad。无论我今天尝试了什么,我都无法让它们相互检测(我尝试过本地 WiFi 和蓝牙)。

这是我的应用程序首次启动时在 UIViewController 中运行的授权代码(始终返回“本地播放器已通过身份验证。”以及 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!")
    }
}

这是我在单独的 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)"
    }
}

无论我用我的两台 iPad(都是 iPad 3 型号)做什么,都不会看到另一台。我是否正确调用 startBrowsingForNearbyPlayersWithHandler?

另外请注意,在上面的授权代码中,我正在使用这篇文章推荐的 Objective-C 解决方法:Game Center not authenticating using Swift,因为这样做的“Swift 方式”对我也不起作用。

我还通过本地 WiFi 在两个设备上运行了 Spelltower,因此看起来硬件运行正常。知道这里可能出了什么问题吗?

【问题讨论】:

  • 你能确定GKMatchmaker.sharedMatchmaker() 正在返回一个共享的媒人实例吗?当GKLocalPlayer.localPlayer() 不存在时,为什么这应该起作用。
  • 它从 NSLog 返回为 ,所以我认为它是。有没有更好的方法来检查?据我了解,Apple 使 GKLocalPlayer.localPlayer() 成为 Obj-C 的公共方法和 Swift 的私有方法,我们都非常困惑为什么。
  • 请问您遇到此问题的 iOS 版本是什么?我以前可以正常工作,但最新的(8.4)我注意到这也不再适合我了。
  • 我已经为此提交了一份雷达文件。如果您决定再试一次,请让我们知道您是否成功。

标签: ios bluetooth swift ios8 gkmatchmaker


【解决方案1】:

我决定放弃通过 Game Center 进行开发,转而使用 Multipeer Connectivity。

【讨论】:

    【解决方案2】:

    您没有注册课程以接收邀请更新。您需要注册一个类并实现符合 GKLocalPlayerListener 协议的方法。请参阅我在这篇文章中的回复(它在 Objective-C 中,但同样的概念适用):

    Some startBrowsingForNearbyPlayersWithReachableHandler questions

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多