0

GKSession 正在搜索自己(同一设备),为什么?即使在 GKRocket 示例中,为什么会这样?

我怎样才能停止不进行自我连接?

4

3 回答 3

1

确保从设备只打开一个会话.. GKSession 查找具有匹配 ID 的会话...如果您从具有相同 ID 的设备创建新会话,它将找到旧会话..

于 2011-05-02T08:26:26.730 回答
0

我在 GKRocket 的修改版本中遇到了这个问题。当对等端断开连接时,应用程序返回到前屏幕,然后重新加载启动会话的视图。

您需要通过在应用程序中尽早实例化创建 GKSession 的类来解决此问题。在关闭应用程序之前,用户不得随时向后导航。然后在整个导航堆栈中维护指向会话控制器类的指针,以便您可以调用对等列表等。

这些方法来自 AppDelegate 之后的第一个视图控制器

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

//We setup the GKSession at this stage so that we do not create duplicate sessions as
//the user navigates to and from the game lobby controller finding and managing peers
manager = [[SessionManager alloc] init];
manager.lobbyDelegate = nil;  //There is no game lobby at this stage so we nil this.
[manager setupSession];
// call the session manager's setup method to create the session. It will start
//looking for peers right away, but we won't see that until we go to the game lobby

}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//Pass a pointer to the session manager along the line of segues
[[segue destinationViewController] setManager:self.manager];
}

此代码是从 GKRocket 修改的 - 查看该教程以了解 setupSession 之类的方法。

于 2013-09-17T17:24:28.527 回答
0

你需要做的是使用:

session.available = NO;

在正在搜索的设备上。搜索结束时只需将其设置为 YES 即可。

如果会话是 AppDelegate 的一部分:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
于 2012-02-14T23:22:18.197 回答