无法将GKTurnBasedEventListener设置为我的ViewController的委托? [英] GKTurnBasedEventListener could not be set to delegate of my ViewController?

查看:83
本文介绍了无法将GKTurnBasedEventListener设置为我的ViewController的委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在objC中,由GKTurnBasedEventListener的> Rawendrich ,当时是GKTurnBasedEventHandler,现在被Apple更改如下.

In objC the syntax written by Rawendrich for GKTurnBasedEventListener, which was GKTurnBasedEventHandler there at that time, now changed by Apple is as below.

 if (!gameCenterAvailable) return;

void (^setGKEventHandlerDelegate)(NSError *) = ^ (NSError *error)
{
    GKTurnBasedEventHandler *ev = 
      [GKTurnBasedEventHandler sharedTurnBasedEventHandler];
    ev.delegate = self;
};

NSLog(@"Authenticating local user...");
if ([GKLocalPlayer localPlayer].authenticated == NO) {     
    [[GKLocalPlayer localPlayer] 
     authenticateWithCompletionHandler:
      setGKEventHandlerDelegate];        
} else {
    NSLog(@"Already authenticated!");
    setGKEventHandlerDelegate(nil);
}

现在,将其转换为快速后,加上写下GKTurnBasedEventListener而不是GKTurnBasedEventHandler的组成,这是通过以下方式实现的.

Now after converting this to swift, and with the composition of writing down GKTurnBasedEventListener instead of GKTurnBasedEventHandler, this comes the following way.

//  Converted with Swiftify v1.0.6381 - https://objectivec2swift.com/
if !gameCenterAvailable {
return
}
var setGKEventHandlerDelegate: ((_: Error) -> Void)? = {(_ error:   Error?) -> Void in
    var ev = GKTurnBasedEventHandler.shared()
    ev.delegate = self
}
print("Authenticating local user...")
if GKLocalPlayer.localPlayer().authenticated == false {
GKLocalPlayer.localPlayer().authenticate(withCompletionHandler:    setGKEventHandlerDelegate)
}
else {
print("Already authenticated!")
setGKEventHandlerDelegate(nil)

}

不幸的是,这不是为我的ViewController设置GKTurnBasedEventListener的委托的正确语法.

Unfortunately this is not the right syntax to set delegate of GKTurnBasedEventListener for my ViewController.

请大家中的任何一个为我解决这个问题,因为没有这个,我将无法通读事件监听器的默认功能.

Please if anyone of you could solve this for me, because without this I'm not able to read through event listener's default functions.

干杯!

推荐答案

最后大约10个小时,我从这里.尽管此语法在objC中使用,但是将其从 Swiftify 转换为swift是没有问题的.

Finally after just about harsh 10 hours, I figured out this problem from Here. Although this syntax is in objC, but there's no problem of converting it to swift from Swiftify.

虽然比实时时间晚一点,但是我现在能够理解,设置GKTunBasedEventListener的委托与我们为UITableViewControllerDelegate所做的委托不一样.

Although a bit later than the real time, but I'm now able to understand that setting delegate of GKTunBasedEventListener is not like the one we do for UITableViewControllerDelegate.

在这里,必须先对本地播放器进行身份验证,然后在将本地播放器的侦听器注册到ViewControllerdelegate GKLocalPlayerListener之后.

Here, one must have to first authenticate local player, then after you have to register local player's listener to the ViewController's delegate GKLocalPlayerListener.

我在 Apple的文档上发现的另一件事: 请勿直接实现GKChallengeListenerGKInviteEventListenerGKSavedGameListenerGKTurnBasedEventListener;改为实施GKLocalPlayerListener.您可以使用GKLocalPlayerListener监听并处理多个事件.

One other thing I found on Apple's Documentation: Do not implement GKChallengeListener, GKInviteEventListener, GKSavedGameListener, and GKTurnBasedEventListener directly; implement GKLocalPlayerListener instead. You can listen for and handle multiple events using GKLocalPlayerListener.

因此,接下来我以以下方式实现.

So then on I've implemented in the following way.

import GameKit

class ViewController: UIViewController,   GKTurnBasedMatchmakerViewControllerDelegate, 
GKLocalPlayerListener {


..... 


func player(_ player: GKPlayer, receivedTurnEventFor match: GKTurnBasedMatch, didBecomeActive: Bool) {

    print("#1")
    print(player)
    print("#2")
    print(match)
    print("#3")
    print(didBecomeActive)

    if match.status == GKTurnBasedMatchStatus.open
    {
        if GKLocalPlayer.localPlayer() == match.currentParticipant
        {


        if didBecomeActive
        {
            // Active now
        }
        else
        {
            // Active already
        }

        }
        else
        {
            // It's someone's turn

            if match.matchData != myMatch?.matchData
            {
               // Match Data being Updated by Someone
                print(player.alias ?? "No Name:")
            }

        }


    }

    thirdTopLabel.text = match.matchID! + "\n" + didBecomeActive.description

}

.... 

现在在ViewDidLoad()函数中输入以下代码.

Now in ViewDidLoad() function put the following code.

// In the ViewDidLoad function 
 if(!GKLocalPlayer.localPlayer().isAuthenticated)
    {
        authenticatePlayer { (auth) in

            weak var weakSelf = self
            weak var weakPlayer = GKLocalPlayer.localPlayer()

            if(auth){


                 weakPlayer?.register(weakSelf!)
                 self.suthentication = true;

            }
            else{
                print("failed in authentication")
                self.suthentication = false;
            }

        }

    }
    else
    {
        // Already Authenticated
        GKLocalPlayer.localPlayer().register(self)
        localPlayer = GKLocalPlayer.localPlayer()


    }

最后,您的身份验证功能应该是这样的.

And finally your Authentication function should be like this.

// authenticate local player :: Just Authentication
func authenticatePlayer(completionHandler: @escaping (_ resultedPlaces: Bool) -> Void) {

    localPlayer = GKLocalPlayer.localPlayer()


    localPlayer.authenticateHandler =
        { (viewController , error ) -> Void in
            if viewController != nil
            {
                self.present(viewController!, animated:true, completion: nil)
            }
            else
            {
                if self.localPlayer.isAuthenticated
                {

                    completionHandler(true);
                }
                else
                {
                    completionHandler(false);


                    print("not able to authenticate fail")
                    self.gameCenterEnabled = false

                    if (error != nil)
                    {
                        print("\(error.debugDescription)")
                    }
                    else
                    {
                        print(    "error is nil")
                    }
                }
            }

    }
}

注意:GKLocalPlayerListener在模拟器上不起作用.

NOTE: GKLocalPlayerListener won't work on simulator.

这篇关于无法将GKTurnBasedEventListener设置为我的ViewController的委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆