简体   繁体   English

不使用GKPickercontroller无法连接两个设备

[英]Not able to connect two devices without using GKPickercontroller

I am trying to connect two devices using Gamekit and transfer small amount of data. 我正在尝试使用Gamekit连接两个设备并传输少量数据。 I was able to do this with the use GKPickerController but now to make the user experience a little better i am trying to achieve this automatically. 我可以使用GKPickerController做到这一点,但现在为了让用户体验更好,我尝试自动实现这一点。 The problem is that the delgate method 问题是delgate方法

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

is not getting called. 没有接到电话。

Also these are two different apps. 另外,这是两个不同的应用程序。

Please find the code below: 请在下面找到代码:

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];

}

Alright..figured this out. 好吧。。 For some reason it wasn't working without session id I put the same session id in both the apps and it worked. 出于某种原因,没有会话ID就无法正常工作,我在两个应用程序中都输入了相同的会话ID,并且它可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以通过获取IP地址来使用套接字连接两个ios设备? - Is it possible to connect two ios devices using sockets by getting their IP addressess? 列出并连接没有MPVolumeView的airplay设备 - List & connect to airplay devices without MPVolumeView 如何在不使用USB线将设备连接到Macbook的情况下,将Apple设备(iPad或iPhone)无线连接到Xcode? - How to wirelessly connect Apple devices (iPad or iPhone) to Xcode without connecting the device to the Macbook using USB cable? 无法使用 Javscript 识别 iPad Pro 设备 - Not able to identify iPad Pro devices by using Javscript 使用Amazon SNS时无法接收到iOS设备的推送通知 - Not able to receive push notification to iOS devices when using Amazon SNS 如何使用airplay以编程方式发现iOS设备列表和Connect for mirroring? - How to programmatically discover the iOS devices list and Connect for mirroring using airplay? 在使用蓝牙连接到设备之前如何要求认证? - How to ask for authentication before connect to devices using bluetooth? 不使用麦克风连接 Twilio - Connect with Twilio without using microphone 我们如何使用BLE将Garmin设备连接到iOS应用程序 - How can we connect garmin devices to iOS Application using BLE 无法使用uipangesturerecogniser移动两个图像 - Not able to move two images using uipangesturerecogniser
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM