ios GameKit 实例(点对点通信)

#import "AppDelegate.h"
#import <GameKit/GameKit.h>

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    
    m_button1 = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];
    m_button2 = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 200, 40)];
    m_label1 = [[UILabel alloc]initWithFrame:CGRectMake(100, 0, 200, 40)];
    m_label2 = [[UILabel alloc]initWithFrame:CGRectMake(100, 300, 200, 40)];
    m_label3 = [[UILabel alloc]initWithFrame:CGRectMake(100, 400, 200, 40)];
    
    
    m_button1.backgroundColor = [UIColor blueColor];
    m_button2.backgroundColor = [UIColor blueColor];
    
    [m_button1 setTitle:@"点击" forState:UIControlStateNormal];
    [m_button2 setTitle:@"连接" forState:UIControlStateNormal];
    
    [m_button1 setEnabled:NO];
    [m_button2 setEnabled:YES];
    
    [m_button2 addTarget:self action:@selector(connection) forControlEvents:UIControlEventTouchDown];
    [m_button2 addTarget:self action:@selector(connection) forControlEvents:UIControlEventTouchDown];
    
    [self.window addSubview:m_button1];
    [self.window addSubview:m_button2];
    [self.window addSubview:m_label1];
    [self.window addSubview:m_label2];
    [self.window addSubview:m_label3];
    
    int count = [m_label1.text intValue];
    m_label1.text = [NSString stringWithFormat:@"%i",++count];
    NSString *send = [NSString stringWithFormat:@"{\"code\":%i,\"count\":%i}",1,count];
    NSData *data= [send dataUsingEncoding:NSUTF8StringEncoding];
    if(_session)
    {
        [_session sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil];
    }
    return YES;
}

-(void)connection
{
    _peerpick = [[GKPeerPickerController alloc]init];
    _peerpick.delegate =self;
    _peerpick.connectionTypesMask = GKPeerPickerConnectionTypeNearby;
    [_peerpick show];
}

-(void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session

{
    NSLog(@"建立连接");
    _session = session;
    _session.delegate = self;
    [_session setDataReceiveHandler:self withContext:Nil];
    _peerpick.delegate = nil;
    [_peerpick dismiss];
    [m_button2 setEnabled:YES];
    [m_button2 setTitle:@"断开连接" forState:UIControlStateNormal];
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(upda) userInfo:nil repeats:YES];
    
    
}
ps:由于缺少实验环境,所以代码有问题主要功能在peerPickerController 和connection中
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值