0
votes

I'am working on a peer to peer chat application in iOS using multipeer connectivity framework, i have setup both advertiser and browser in my app. Here is the advertiser and initialisation code:

 _myDisplayname = [[UIDevice currentDevice] name]; _mypeer =[[MCPeerID alloc]initWithDisplayName:_myDisplayname];

_mySession = [[MCSession alloc]initWithPeer:_mypeer securityIdentity:nil encryptionPreference:MCEncryptionRequired];
self.mySession.delegate = self;
_assistant = [[MCAdvertiserAssistant alloc]initWithServiceType:serviceType    discoveryInfo:nil session:_mySession]; [_assistant start]; _nearbyBrowser = [[MCNearbyServiceBrowser alloc]initWithPeer:_mypeer serviceType:serviceType];
self.advertiser = [[MCNearbyServiceAdvertiser alloc]initWithPeer:self.mypeer discoveryInfo:nil serviceType:@"blue-chat"];

-I have advertised the service using MCAdvertiserAssistant in viewDidLoad and browsing code is as follows :

 MCBrowserViewController *browser = [[MCBrowserViewController alloc]initWithBrowser:self.nearbyBrowser session:self.mySession];
browser.delegate =self;
[self presentViewController:browser animated:YES completion:nil];
  • I have used the same session object for both advertiser and browser, When launched fresh on two iOS devices (iPhone 4s and iPhone 6 ) ,the two devices get connected and chat works but after some time of chatting the the peer is disconnected, also sometimes within a minute of making connection, the peers get disconnected. Any help is appreciated. Thanks!!
1
yes its completely solved now !! - Manoj Karki
How did you solve this? Please! I need help - Pablo Martinez

1 Answers

1
votes

In the session delegate set the certificateHandler to true, if the MCSessionState changes directly from *Connecting* to *Not Connected* State

public func session(session: MCSession, didReceiveCertificate certificate: [AnyObject]?, fromPeer peerID: MCPeerID, certificateHandler: (Bool) -> Void)
 {
  certificateHandler(true)
 }

It worked for me.

Also if it disconnects after sometime, check your initialisation of session, peerID, browser and advertiser variables

var myOwnPeerId : MCPeerID!

// For finding the devices
var serviceAdvertiser : MCNearbyServiceAdvertiser!

// For listening to devices
var serviceBrowser : MCNearbyServiceBrowser!

First initialise the myOwnPeerId and use those instance in the serviceBrower and serviceAdvertiser variable initialisation