在的GameKit iPhone SDK的3.0 [英] GameKit in iPhone SDK 3.0

查看:243
本文介绍了在的GameKit iPhone SDK的3.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的需求的使用<一个href=\"http://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/GameKit_Guide/FindingPeerswithPeerPicker/FindingPeerswithPeerPicker.html\"相对=nofollow>同行选择器找到同行在新的iPhone SDK 3.0?

Do I need to use the Peer Picker to find peers in the new iPhone SDK 3.0?

我真的不希望使用它,但我不希望使用对等网络蓝牙连接。有什么样code,演示没有使用同行选择器的蓝牙连接?本场比赛 GKTank ,苹果提供的使用同行选择器,所以我不能使用的。

I don't really want to use it, but I do want to use the peer-to-peer Bluetooth connection. Is there any sample code that demonstrates the Bluetooth connection without using Peer Picker? The game GKTank that Apple provides uses the Peer Picker, so I can't use that.

推荐答案

有两种方法可以做到这一点。

There are two ways to do this.

第一种方法使用的GameKit API。您可以通过有两个单独的类,一个实现了 GKSessionDelegate 的协议,并充当的GameKit /蓝牙处理程序,另一个为presentation UI启动(最有可能的某种ViewController中的一个实现代码如下)。你会连线它的方式是处理程序管理的GameKit通知等,然后调用UI委托方法时,对等连接/脱落等,这种方式更新表来看,由于设备来来去去,你的选择器列表应该更新,以显示谁的身边。

The first way uses GameKit API. You start by having two separate classes, one that implements the GKSessionDelegate protocol and acts as a GameKit/Bluetooth "handler" and the other as the presentation UI (most likely some sort of viewcontroller with a tableview). The way you would wire it up is the handler manages the GameKit notifications etc and then calls delegate methods on the UI to update the table view when a peer connects/drops off, etc. That way, as devices come and go, your picker list should update to show who's around.

下面是一些code,让你开始:

Below is some code to get you started:

- (BOOL) startPeer
{
    BOOL result = NO;

    if (!_session) {
    	_session = [[GKSession alloc] initWithSessionID:BLUETOOTHSESSION 
    											displayName:nil 
    											sessionMode:GKSessionModePeer];
    	_session.delegate = self;
    	[_session setDataReceiveHandler:self withContext:nil];
    	_session.available = YES;
	result = YES;
    }
    return result;
}

- (void) stopPeer
{
    // Set up the session for the next connection
    //
    [_session disconnectFromAllPeers];
    _session.available = YES;

    [self cleanupProgressWindow];
}

- (void) loadPeerList 
{
    self.peerList = [[NSMutableArray alloc] initWithArray:[_session peersWithConnectionState:GKPeerStateAvailable]];
}


- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state
{
    BOOL peerChanged = NO;

    switch(state) {

    	// When peer list changes, we adjust the available list
    	//
    	case GKPeerStateAvailable:
    		if (_peerList) {
    			[_peerList addObject:peerID];
    			peerChanged = YES;
    		}
    		break;

    	// When peer list changes, we adjust the available list
    	//
    	case GKPeerStateUnavailable:
    		if (_peerList) {
    			[_peerList removeObject:peerID];
    			peerChanged = YES;
    		}
    		break;


    	// Called when the peer has connected to us.
    	//
    	case GKPeerStateConnected:
                    // start reading and writing
    		break;

        case GKPeerStateDisconnected:
    	{
    		if (_isWriter) {
    			_isConnected = NO;
    			_deviceToSend = nil;
    			[self cleanupProgressWindow];
    		} else {
    			// Other side dropped, clean up local data and reset for next connection
    			self.dataRead = nil;
    		}
    	}
    		break;
    }

    // Notify peer list delegate that the list has changed so they can update the UI
    //
    if (peerChanged)
    	CALLDELEGATE(_peerListDelegate, peerListChanged);			
}

要做到这一点的第二种方法是使用标准的Bonjour服务选择机制。的GameKit是在卓悦的基础上实现(但在蓝牙,而不是无线),所以一旦双方通过网络可达性了相互连接它们的Bonjour下注册,并像对待任何Bonjour服务。该方法的GameKit可能是更容易一些,但如果你已经有code支持WiFi可以重复使用蓝牙以及

The second way to do it is to use standard Bonjour service selection mechanisms. GameKit is implemented on top of Bonjour (but over Bluetooth instead of WiFi) so once the two sides have gone through network reachability with each other and connected they are registered under Bonjour and act like any Bonjour service would. The GameKit way is probably a little easier, but if you already have code for WiFi it can be reused for Bluetooth as well.

这篇关于在的GameKit iPhone SDK的3.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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