Manoj Karki
Manoj Karki

Reputation: 270

Disconnect issue in multipeer framework iOS 9.2

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];

Upvotes: 0

Views: 393

Answers (1)

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

Upvotes: 1

Related Questions