iOS多点连接显示相同的设备名称两次 [英] iOS Multi peer connectivity showing same device name twice

查看:129
本文介绍了iOS多点连接显示相同的设备名称两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用iOS 7多点技术连接我的iPad和iPod touch。但是当iPod touch或iPad进入休眠状态时,它会断开连接,这是很好,因为多对等不工作在后台模式,但是当我再次发现它显示iPods名称两次在 MCBrowserViewController 列表。尝试这与每个示例代码和每个代码有相同的问题,任何人都知道如何解决这个错误。

I am using iOS 7 multi peer technology for connecting my iPad and iPod touch. But whenever iPod touch or iPad goes to sleep it gets disconnected which is fine because multi peer dont work in background mode, but when i discover again it shows iPods name twice in the MCBrowserViewController list. Tried this with every sample code and every code has same issue any one know how to fix this bug.

如果我连接设备并且其他设备接受它,即使 MCBrowserViewController 连接 MCBrowserViewController 仍将显示为连接,完成按钮被禁用。我使用 MCBrowserViewController ,没有自定义代码,所以我猜这是从苹果的问题。

Also there is one weird issue with MCBrowserViewController if i connect a device and other device accepts it, even though it gets connected MCBrowserViewController will still show as connecting and "Done" button is disabled. I am using MCBrowserViewController and no custom code for this so i guess this is issue from apple.

推荐答案

发现您的同一个名称两次是什么时候才知道如何直接连接到设备?因为你在每次初始化会话时都会initpeerID(withDisplayName)。
从苹果的文档,这是一个已知的错误,你不应该这样做。相反,将你的peerID保存在某个地方(例如NSUserDefaults),当你初始化会话时,验证peerID是否存在,加载它,否则创建/保存它。

Discovering your same name twice is because you do "init" the peerID (withDisplayName) each time you init your session. From apple's documentation, it's a known bug and you should not do so. Rather, save your peerID somewhere (such as NSUserDefaults), and when you init your session, verify if peerID exists, load it, else create/save it.

代码将如下所示:
在会话的init中,替换:

The simplest code will look like this: In the init of your session, replace:

_peerID = [[MCPeerID alloc] initWithDisplayName:XXX];

由:

//If there is no PeerID save, create one and save it
if ([[NSUserDefaults standardUserDefaults] dataForKey:@"PeerID"] == nil)
{
    _peerID = [[MCPeerID alloc] initWithDisplayName:XXX];
    [[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:_peerID] forKey:@"PeerID"];
}
//Else, load it
else
{
    _peerID            = [NSKeyedUnarchiver unarchiveObjectWithData:[[NSUserDefaults standardUserDefaults] dataForKey:@"PeerID"]];
}

当然,你可以创建一个更复杂的代码,它来自动态变量,以防您更改名称等。

Of course, you can make a more sophisticated code, such deallocating it and create it from a dynamic variable in case you wanna change name, etc.

这篇关于iOS多点连接显示相同的设备名称两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆