0

我正在尝试使用 Gamekit 连接两个设备并传输少量数据。我可以使用 GKPickerController 做到这一点,但现在为了让用户体验更好一点,我正在尝试自动实现这一点。问题是delgate方法

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state

没有被调用。

这也是两个不同的应用程序。

请在下面找到代码:

iPad app 

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate,GKSessionDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) GKSession *connectSession;
@end

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    _peerDevice = [[NSMutableArray alloc]init];

    _connectSession = [[GKSession alloc]initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer];
    _connectSession.delegate = self;
    _connectSession.available = YES;
    _connectSession.disconnectTimeout = 0;
    [_connectSession setDataReceiveHandler:self withContext:nil];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[xyz alloc] initWithNibName:@"xyz" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{


    if(state == GKPeerStateConnected){
        // Add the peer to the Array
        [_peerDevice addObject:peerID];

        NSString *str = [NSString stringWithFormat:@"Connected with %@",[session displayNameForPeer:peerID]];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Connected" message:str delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];

        // Used to acknowledge that we will be sending data
        [session setDataReceiveHandler:self withContext:nil];

        [[self.window viewWithTag:12] removeFromSuperview];

    }

}

- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID
{
    NSLog(@"didReceiveConnectionRequestFromPeer: %@", [session displayNameForPeer:peerID]);

    [session acceptConnectionFromPeer:peerID error:nil];

}

iPhone app

AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate, GKSessionDelegate,UIAlertViewDelegate>
@property (nonatomic, strong) GKSession *connectSession;
@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    _peerDevice = [[NSMutableArray alloc]init];

    _connectSession = [[GKSession alloc]initWithSessionID:nil displayName:nil sessionMode:GKSessionModePeer];
    _connectSession.delegate = self;
    _connectSession.available = YES;
    _connectSession.disconnectTimeout = 0;

    [_connectSession setDataReceiveHandler:self withContext:nil];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
     _nav = [[UINavigationController alloc]initWithNavigationBarClass:[CustomNavigationBar class] toolbarClass:nil];
    self.viewController = [[HCAViewController alloc] initWithNibName:@"HCAViewController" bundle:nil];
    [_nav pushViewController:self.viewController animated:YES];
    self.window.rootViewController = _nav;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)session:(GKSession *)session peer:(NSString *)peerID didChangeState:(GKPeerConnectionState)state{

    _connectSession = session;
    NSString * str;
    UIAlertView * alert;

    switch (state) {

        case GKPeerStateAvailable:
            [session connectToPeer:peerID withTimeout:0];
            break;

        case GKPeerStateConnected:
            [_peerDevice addObject:peerID];

            str = [NSString stringWithFormat:@"Connected with SFMedical"];
            alert = [[UIAlertView alloc] initWithTitle:@"Connected" message:str delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];

            // Used to acknowledge that we will be sending data
            [session setDataReceiveHandler:self withContext:nil];

            [[self.window viewWithTag:12] removeFromSuperview];
            break;

        default:
            break;
    }


}

- (void)session:(GKSession *)session didReceiveConnectionRequestFromPeer:(NSString *)peerID
{
    NSLog(@"didReceiveConnectionRequestFromPeer: %@", [session displayNameForPeer:peerID]);

    [session acceptConnectionFromPeer:peerID error:nil];

}
4

1 回答 1

0

好吧..想通了。出于某种原因,如果没有会话 ID,它就无法工作,我在两个应用程序中都放置了相同的会话 ID,并且它可以工作。

于 2013-06-13T17:12:34.223 回答