I have a MultipeerService class which is used to start advertising and browsing sessions. For some reason I am not sure why I am not able to see any advertisers.
MultipeerService.m
-(void) startAdvertising
{
NSString *name = [[UIDevice currentDevice] name];
MCPeerID *peerId = [[MCPeerID alloc] initWithDisplayName:name];
self.session = [[MCSession alloc] initWithPeer:peerId];
self.session.delegate = self;
self.advertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:peerId discoveryInfo:nil serviceType:kServiceType];
self.advertiser.delegate = self;
[self.advertiser startAdvertisingPeer];
}
-(void) startBrowsing
{
NSString *name = [[UIDevice currentDevice] name];
MCPeerID *peerId = [[MCPeerID alloc] initWithDisplayName:name];
self.session = [[MCSession alloc] initWithPeer:peerId];
self.session.delegate = self;
self.browser = [[MCNearbyServiceBrowser alloc] initWithPeer:peerId serviceType:kServiceType];
self.browser.delegate = self;
[self.browser startBrowsingForPeers];
}
I start the advertiser like the following:
_multipeerConnectivityService = [[MultipeerConnectivityService alloc] init];
[_multipeerConnectivityService startAdvertising];
I create a new instance of multipeerConnectivityService for browsing and invoke the startBrowsing method.
When I check in the foundPeer method in the multipeerConnectivityService I see nothing invoked. What am I doing wrong?
You should implement the browser:didNotStartBrowsingForPeers:
delegate method. If it is invoked, the NSError
object you receive will help you diagnose the problem.
- (void)browser:(MCNearbyServiceBrowser *)browser didNotStartBrowsingForPeers:(NSError *)error
{
NSLog( @"Unable to start browsing for peers. Error: %@", error );
}
Make sure everything is a property. Even the custom classes that you made for encapsulating the multipeer connectivity framework.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With