问题
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.
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.
Also any one knows how to directly connect to the device when app comes back to active state from sleep mode?
回答1:
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.
The simplest code will look like this: In the init of your session, replace:
_peerID = [[MCPeerID alloc] initWithDisplayName:XXX];
by:
//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.
回答2:
I had the same issue and this is how I solved it,, In my case I used a UIViewController to handle the connections and every time I open the view I alloc and init the view -viewDidLoad will be called each time- , then in viewDidLoad I initial the MCPeerID & MCSession and thats the problem and this is why we see multi peer connectivity showing twice, so I solved it by doing the initialisation of MCPeerID & MCSession only once in the AppDelegate or a global Class.
来源:https://stackoverflow.com/questions/23235487/ios-multi-peer-connectivity-showing-same-device-name-twice