我正在制作一个使用多点连接框架的iOS 7应用程序,但我无法让两个设备相互识别。我已经浏览了文档和wwdc视频,除此之外,关于这个框架的信息非常有限。有没有人有使用这种新的点对点功能的经验并能提供帮助?
这是我到目前为止的基本情况。browserVC显示在屏幕上,但当我在两个设备上运行该应用程序时,找不到任何设备。
MCPeerID *peer = [[MCPeerID alloc] initWithDisplayName:@"user"];
_session = [[MCSession alloc] initWithPeer:peer];
NSString *service = @"nsync";
_session.delegate = self;
MCAdvertiserAssistant *assistant =[[MCAdvertiserAssistant alloc] initWithServiceType:service
discoveryInfo:nil
session:_session];
[assistant start];
MCBrowserViewController *browserVC = [[MCBrowserViewController alloc] initWithServiceType:service
session:_session];
browserVC.delegate = self;
[self presentViewController:browserVC
animated:YES
completion:nil];发布于 2013-09-19 06:40:59
要让你的浏览器看到设备,你需要有一些其他的设备在做广告。我认为你的问题是你的MCAdvertiserAssistant超出了作用域并且被释放了,因为它只存储在一个局部变量中。
下面是我用来做广告的代码:
#define SERVICE_TYPE @"MyServiceType"
...
@property (nonatomic, strong) MCAdvertiserAssistant* advertiserAssistant;
...
self.peerId = [[MCPeerID alloc] initWithDisplayName:[[UIDevice currentDevice] name]];
self.advertiserSession = [[MCSession alloc] initWithPeer:self.peerId];
self.advertiserSession.delegate = self;
self.advertiserAssistant = [[MCAdvertiserAssistant alloc] initWithServiceType:SERVICE_TYPE discoveryInfo:nil session:self.advertiserSession];
[self.advertiserAssistant start];请注意,我将广告商助手存储在一个属性中,这样在创建它的方法完成后,它就不会被释放。
和浏览:
self.peerId = [[MCPeerID alloc] initWithDisplayName:[[UIDevice currentDevice] name]];
self.browserSession = [[MCSession alloc] initWithPeer:self.peerId];
self.browserSession.delegate = self;
self.browser = [[MCBrowserViewController alloc] initWithServiceType:SERVICE_TYPE session:self.browserSession];
self.browser.delegate = self;
[self presentViewController:self.browser animated:YES completion:nil];发布于 2013-08-01 22:42:44
不要将MCAdvertiserAssistant *assistant声明为局部变量,而应声明为类成员。
发布于 2013-09-19 19:45:54
我同意。
我昨天刚试了一下,它就像一个护身符。除了MCAdvertiserAssistant之外,您的代码似乎是正确的。应该设置为全局变量!
正如Greg所说,你必须在2台至少连接wifi或蓝牙的设备上运行你的应用程序(不需要互联网连接)。请注意,它在蜂窝网络中不起作用。
https://stackoverflow.com/questions/17978670
复制相似问题