如何从Game Center获取当前用户的姓名

时间:2016-05-29 00:09:45

标签: ios game-center

当经过身份验证的处理程序从Game Center返回时触发,本地播放器列出displayName =“Me”,别名是玩家的用户名。但是我想显示用户的全名,所以想要实际的displayName,而不是“Me”。

有没有办法指定我想要全名,而不是“我”?

1 个答案:

答案 0 :(得分:0)

我更喜欢显示别名,尤其是在与随机播放器匹配时,别名和displayName都要求玩家的身份验证完成后才能开始返回相应的值。

要启动身份验证过程,您必须设置本地播放器的身份验证处理程序。只需设置它就会启动该过程,并在几秒钟内调用该方法。之后,本地玩家的别名和displayName应该是正确的。

例如:

 class YourGameCenterManager:GKGameCenterControllerDelegate,
                    GKLocalPlayerListener
 {
   var localGCAccount: GKLocalPlayer!
   var active               = false

   init()
   { 
     localGCAccount     = GKLocalPlayer.localPlayer()
     localGCAccount?.authenticateHandler = gameCenterAuthentication
   }

   func gameCenterAuthentication(gameCenterVC :UIViewController?, err:NSError?)
   {    
      if gameCenterVC != nil
      {       
        // Game center wants to display a sign-on view ...
        // note: I personally never got this to actually happen
      }
      else if localGCAccount?.authenticated ?? false
      {
         if not(active)
         {
           active = true
           localGCAccount?.unregisterAllListeners()
           localGCAccount?.registerListener(self) 

           // ... whatever else you need to do when Game Center is ready
           // at this point localGCAccount's alias and displayName should be ok
         }
      }
      else if active
      {   
        //... Game Center just went bad ... do what you have to to to handle it    
        active = false      
      }         
   }