ios GameKit 实例(点对点通信)
2014-02-19 11:21
260 查看
#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中
相关文章推荐
- iOS GameKit蓝牙通信以及自定义cell的使用--实现仿微信聊天功能
- ios 基于GameKit框架的蓝牙通信
- 浅谈iOS中的蓝牙技术(一) --GameKit.framework
- iOS 蓝牙(GameKit CoreBluetooth)
- iOS - 蓝牙技术(一) - GameKit框架
- Objective-C(十八、谓语使用及实例说明)——iOS开发基础
- IOS开发之小实例--创建一个简单的用于视频录制和回放的应用程序
- 【iOS开发必收藏】详解iOS应用程序内使用IAP/StoreKit付费、沙盒(SandBox)测试、创建测试账号流程!【2012-12-11日更新获取"产品付费数量等于0的问题"】
- iOS - Swift 如何使用Masonry和SnapKit
- iOS Sprite Kit最新特性Physics Field虚拟物理场Swift测试
- 【代码笔记】iOS-JSONKit的使用
- 实例:iOS 中的 JS 交互 OC & Swift 双语
- iOS之MJRefresh下拉刷新和上啦加载更多实例
- IOS 如何保存图片到相册实例
- iOS Sprite Kit教程之滚动场景
- iOS runtime的应用实例
- iOS中只让textField使用键盘通知的实例代码
- IOS 网络浅析-(七 JSON解析之三方JSONKit)
- ios 单利的完整使用实例 及销毁 宏定义
- iOS开发中 类、对象、实例变量、成员变量、属性变量等区别和关系