两个iOS互相传输数据有这么几种方式:
1、蓝牙
2、wifi
3、AirDrop
4、近场传输
这里讲述的是最后一个,虽然使用场景不像前几种广泛,但是用途还是不小的。
它具有如下特点:传输范围比蓝牙高,不依赖网络(但是要打开WiFi开关或蓝牙开关)。
AppStore上有一个应用名字叫做firechat,这个就是利用了这个技术,试想一个场景,如果发生自然灾害,那么可能会毁坏公共设置,这个时候想要互相交流,就有这个技术发挥的用场了,当然前提是大家都得装上了这个应用
下面开始卖弄技术了。
首先要导入MultipeerConnectivity.framework这个框架,这个技术定义了两个角色,一个是搜索端,一个是被搜索端,先从被搜索端开始。
先定义了如下几个变量
MCPeerID *_peerID; //事物ID
MCSession *mcSession; //连接关系
MCAdvertiserAssistant *adVertiserAssistant; //处理连接邀请和拒绝邀请
MCBrowserViewController *browser; //设备查找控制器
NSMutableArray *peerIDArray; //包含所有设备的数组
BOOL searchClient;
在然后在初始化时进行如下操作
_peerID = [[MCPeerID alloc] initWithDisplayName:[[UIDevice currentDevice] name]];
mcSession = [[MCSession alloc] initWithPeer:_peerID];
[mcSession setDelegate:self];
peerIDArray = [[NSMutableArray alloc] init];
searchClient = YES;
if (adVertiserAssistant == nil)
{
adVertiserAssistant = [[MCAdvertiserAssistant alloc] initWithServiceType:SERVICE_TYPE discoveryInfo:nil session:mcSession];
[adVertiserAssistant start];
}
MCBrowserViewController *browser; //设备查找控制器
searchClient = NO;
if (browser == nil)
{
browser = [[MCBrowserViewController alloc] initWithServiceType:SERVICE_TYPE session:mcSession];
[browser setDelegate:self];
}
[self presentViewController:browser animated:YES completion:nil];
#pragma mark MCSessionDelegate
-(void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state
{
switch (state) {
case MCSessionStateNotConnected:
NSLog(@"没链接上");
[mcSession disconnect];
break;
case MCSessionStateConnecting:
NSLog(@"正在链接");
break;
case MCSessionStateConnected:
{
NSLog(@"已经链接上");
[peerIDArray addObject:peerID];
break;
}
default:
break;
}
}
-(void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID
{
NSString *name = peerID.displayName;
NSLog(@"接受到了数据来自:%@",name);
}
-(void)session:(MCSession *)session didReceiveStream:(NSInputStream *)stream withName:(NSString *)streamName fromPeer:(MCPeerID *)peerID
{
NSLog(@"接受到了流:%@",streamName);
}
-(void)session:(MCSession *)session didStartReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID withProgress:(NSProgress *)progress
{
NSLog(@"开始接受来源:%@",resourceName);
}
-(void)session:(MCSession *)session didFinishReceivingResourceWithName:(NSString *)resourceName fromPeer:(MCPeerID *)peerID atURL:(NSURL *)localURL withError:(NSError *)error
{
NSLog(@"可能是接受完数据吧%@",resourceName);
}
#pragma mark MCBrowserViewControllerDelegate
-(void)browserViewControllerDidFinish:(MCBrowserViewController *)browserViewController
{
NSLog(@"结束");
[browserViewController dismissViewControllerAnimated:YES completion:nil];
}
-(void)browserViewControllerWasCancelled:(MCBrowserViewController *)browserViewController
{
NSLog(@"取消了");
[browserViewController dismissViewControllerAnimated:YES completion:nil];
}
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:(NSJSONWritingPrettyPrinted) error:nil];
BOOL success = [mcSession sendData:data toPeers:peerIDArray withMode:(MCSessionSendDataReliable) error:nil];
if (success)
{
NSLog(@"发送成功:%@",dict);
}
else
{
NSLog(@"发送失败:%@",dict);
}