#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中
#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中