今天我們來(lái)學(xué)習(xí)GameKit用法
邏輯
1.創(chuàng)建附近設(shè)備搜索框
2.設(shè)置提示框代理
3.調(diào)用方法展示提示框
4.實(shí)現(xiàn)代理方法
5.在代理方法中對(duì)傳輸數(shù)據(jù)進(jìn)行處理代碼演示
1.在ViewController.m中導(dǎo)入
#import <GameKit/GameKit.h>
2.創(chuàng)建ui 搜索設(shè)備的btn捆等、發(fā)送數(shù)據(jù)的btn栋烤、展示數(shù)據(jù)的imageView
<pre>
@property (nonatomic, strong) UIButton *clinkBtn;//建立連接
@property (nonatomic, strong) UIButton sendBtn;//發(fā)送數(shù)據(jù)
@property (nonatomic, strong) UIImageView imageView;//顯示數(shù)據(jù)
@property (nonatomic, strong) GKSession session;/會(huì)話/
// 注意UI懶加載代碼神略了哦
-(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self setUpSubViews];
}
-(void)setUpSubViews{
[self.view addSubview:self.clinkBtn];
[self.view addSubview:self.sendBtn];
[self.view addSubview:self.imageView];
}
</pre>
3.在clinkBtn的點(diǎn)擊方法調(diào)用時(shí)創(chuàng)建附近設(shè)備的搜索提示框买窟,并設(shè)置代理,并遵守協(xié)議<GKPeerPickerControllerDelegate>
-(void)clinkBtnAction{
//創(chuàng)建一個(gè)附近設(shè)備的搜索提示框
GKPeerPickerController *ppc = [[GKPeerPickerController alloc]init];//適配6.0,把版本號(hào)改成6.0始绍,就不會(huì)再報(bào)警告
// 如果要搜索到設(shè)備疆虚,還要用到代理方法
ppc.delegate = self;
[ppc show];
}
此時(shí),我們運(yùn)行代碼
點(diǎn)擊按鈕 建立連接
出現(xiàn)搜索設(shè)備的彈框,因?yàn)镚ameKit的局限性:
就是搜索設(shè)備功能只能搜索到 打開(kāi)藍(lán)牙 運(yùn)行同一個(gè)app并點(diǎn)擊 建立連接按鈕的設(shè)備缠捌,所以此時(shí)我們運(yùn)行在模擬器上的demo會(huì)一直處于搜索設(shè)備的狀態(tài)
那么曼月,我們把項(xiàng)目運(yùn)行到真機(jī)上:讓真機(jī)和模擬器進(jìn)行數(shù)據(jù)傳遞柔昼。左邊是真機(jī)捕透,右邊是模擬器乙嘀。
-
此時(shí)虎谢,兩邊都點(diǎn)擊建立連接
屏幕快照 2016-11-27 下午9.12.57.png
這時(shí)候我們就可就可以點(diǎn)擊模擬器上搜索到的iPhone擎场,或者點(diǎn)擊真機(jī)上搜索到的MacBookAir顶籽,進(jìn)行數(shù)據(jù)傳遞了礼饱。
4.設(shè)置傳遞的數(shù)據(jù)匀伏,這里我們?cè)O(shè)置一張圖片到imageView上蝴韭,首先給imageView添加手勢(shì)榄鉴,點(diǎn)擊imageView打開(kāi)相冊(cè),選擇相冊(cè)一張圖片后把此圖設(shè)置到imageView上顯示剃诅。
- 添加手勢(shì)
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapaction)];
[_imageView addGestureRecognizer:tap];
- 實(shí)現(xiàn)手勢(shì)調(diào)用方法矛辕,并簽署相冊(cè)協(xié)議<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
-(void)tapaction{
// 判斷是否支持相冊(cè)選擇
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
NSLog(@"木有相冊(cè)");
return;
}
//創(chuàng)建選擇相片的控制器
UIImagePickerController *pic = [[UIImagePickerController alloc]init];
pic.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
pic.delegate = self;
[self presentViewController:pic animated:YES completion:nil];
}
-
實(shí)現(xiàn)相冊(cè)代理方法
<pre>
// 圖片選擇完之后調(diào)用的方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{[picker dismissViewControllerAnimated:YES completion:nil];
NSLog(@"%s,line = %d,info = %@",func,LINE,info);
//把圖片設(shè)置到imageView上顯示
self.imageView.image = info[UIImagePickerControllerOriginalImage];
}
</pre>
5.實(shí)現(xiàn)點(diǎn)擊 發(fā)送按鈕方法 sendBtn;//發(fā)送數(shù)據(jù)
-(void)sendBtnAction{
//第一步判斷要發(fā)送的數(shù)據(jù)是否存在
if (!self.imageView.image) return;
// 發(fā)送數(shù)據(jù)
// [self.session sendData:UIImagePNGRepresentation(self.imageView.image) toPeers:<#(NSArray *)#>//已經(jīng)連接的所有設(shè)備
// withDataMode:<#(GKSendDataMode)#> error:<#(NSError *__autoreleasing *)#>]
NSError *error = nil;
BOOL sendState = [self.session sendDataToAllPeers:UIImagePNGRepresentation(self.imageView.image)
withDataMode:GKSendDataReliable//GKSendDataReliable可靠地傳輸方式,慢翻屈,不會(huì)丟包擦剑,直到傳完,傳遞的信息完整;GKSendDataUnreliable快纠屋,丟包盾计,可能傳不完族铆,信息可能不完整
error:&error];
if (!sendState) {
NSLog(@"%@",error.localizedDescription);
}
}
6.處理數(shù)據(jù)傳遞哥攘,實(shí)現(xiàn)代理方法
#pragma mark - GKPeerPickerControllerDelegate
//最長(zhǎng)用的方法耕姊,已經(jīng)成功連接到某個(gè)設(shè)備茉兰,并且開(kāi)啟了連接會(huì)話
- (void)peerPickerController:(GKPeerPickerController *)picker //搜索框
didConnectPeer:(NSString *)peerID //設(shè)備id规脸,連接的設(shè)備
toSession:(GKSession *)session //連接會(huì)話莫鸭,通過(guò)會(huì)話可以通過(guò)數(shù)據(jù)傳輸
{
NSLog(@"%s,line = %d",__func__,__LINE__);
// 1. 首先讓搜索彈框消失
[picker dismiss];
// 2. 記錄會(huì)話信息
self.session = session;
// 3.傳輸數(shù)據(jù)
//4.設(shè)置接收數(shù)據(jù),設(shè)置完接受者之后黔龟,接收數(shù)據(jù)會(huì)觸發(fā)方法receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context 現(xiàn)在蘋(píng)果已經(jīng)把這個(gè)方法干掉氏身,就是告訴用戶不要用這個(gè)框架了蛋欣;現(xiàn)在我們只是了解學(xué)習(xí)
[self.session setDataReceiveHandler:self withContext:nil];
}
// 設(shè)置接收數(shù)據(jù),設(shè)置完接受者之后,接收數(shù)據(jù)會(huì)觸發(fā)此方法如贷,現(xiàn)在蘋(píng)果已經(jīng)把此方法干掉陷虎,就是告訴用戶不要用這個(gè)框架了;現(xiàn)在我們只是了解學(xué)習(xí)
- (void)receiveData:(NSData *)data//數(shù)據(jù)
fromPeer:(NSString *)peer//來(lái)自哪個(gè)設(shè)備
inSession:(GKSession *)session//連接會(huì)話
context:(void *)context//
{
NSLog(@"%s,line = %d,data = %@,peer = %@,session = %@",__func__,__LINE__,data,peer,session);
// 將接收的數(shù)據(jù)展示在屏幕上
self.imageView.image = [UIImage imageWithData:data];
}