【问题标题】:GKSession - kill and renew sessionGKSession - 终止和更新会话
【发布时间】:2012-02-21 08:04:17
【问题描述】:

为了读取对等点的新显示名称,我需要终止并更新 GKSession。将其设置为 nil 并重新启动它不起作用。在下面的代码中,for-loop 中显示可用对等点的 NSLog 未被调用(没有错误消息):

-(IBAction) btnRefresh:(id) sender {

    self.currentSession = nil;

    self.currentSession = [[GKSession alloc] initWithSessionID:@"anything" displayName:name sessionMode:GKSessionModePeer];
    self.currentSession.delegate = self;
    self.currentSession.available = YES;
    self.currentSession.disconnectTimeout = 0;
    [self.currentSession setDataReceiveHandler:self withContext:nil];

    peerListAvailable = [[NSMutableArray alloc] initWithArray:[currentSession peersWithConnectionState:GKPeerStateAvailable]];

    for (NSString *peer in peerListAvailable) {
       NSLog(@"found available peer; checking name and ID... %@, %@",[currentSession displayNameForPeer:peer], peer);
    }

将 currentSession 设置为 nil 并重新启动它有什么问题? 也许您知道另一种更新 GKSession 的方法? 提前非常感谢。

【问题讨论】:

    标签: ios bluetooth gksession


    【解决方案1】:

    以下方法说明GKSession设置和拆卸:

    - (void)setupSession
    {
        gkSession = [[GKSession alloc] initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer];
        gkSession.delegate = self;
        gkSession.disconnectTimeout = 5;
        gkSession.available = YES;
    }
    
    - (void)teardownSession
    {
        gkSession.available = NO;
        [gkSession disconnectFromAllPeers];
    }
    

    如果您有兴趣深入研究,请查看GKSessionP2P,这是一个演示应用程序,展示了GKSession 的临时网络功能。该应用程序既在本地网络上宣传自己,又自动连接到可用的对等点,从而建立一个点对点网络。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-16
      • 2014-08-24
      相关资源
      最近更新 更多