/////////////////
-(id)init{
self = [super init];
if (self) {
_peerID = nil;
_session = nil;
_browser = nil;
_advertiser = nil;
}
return self;
}
-(void)setupPeerAndSessionWithDisplayName:(NSString *)displayName{
self.peerID = [[MCPeerID alloc] initWithDisplayName:displayName];
self.session = [[MCSession alloc] initWithPeer:self.peerID];
self.session.delegate = self;
}
-(void)setupMCBrowser{
self.browser = [[MCBrowserViewController alloc] initWithServiceType:@"game-stuff" session:_session];
}
-(void)advertiseSelf:(BOOL)shouldAdvertise{
if(shouldAdvertise) {
self.advertiser = [[MCAdvertiserAssistant alloc] initWithServiceType:@"game-stuff" discoveryInfo:nil session:self.session];
[self.advertiser start];
}
else{
[self.advertiser stop];
_advertiser = nil;
}
}
-(void)browser:(MCNearbyServiceBrowser *)browser foundPeer:(nonnull MCPeerID *)peerID withDiscoveryInfo:(nullable NSDictionary<NSString *,NSString *> *)info
{
}
- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state {
NSDictionary *dict = @{@"peerID":peerID, @"state" : @(state)};//[NSNumber numberWithDouble:state]};
[[NSNotificationCenter defaultCenter] postNotificationName:@"MCDidChangeStateNotification" object:nil userInfo:dict];
}