Search code examples
iosswiftios8multipeer-connectivitymcsession

Multipeer Connectivity: Invalid service type


I am trying to use the Multipeer Connectivity framework but I am getting a crash while trying to instantiate MCNearbyServiceBrowser with a serviceType called "stc-classroom-vik".

Here is the code:

private func setUpSession() {
    self.session = MCSession(peer: self.peerId);
    self.session!.delegate = self;
    
    self.browser = MCNearbyServiceBrowser(peer: self.peerId, serviceType: "stc-classroom-vik");
    self.browser!.delegate =  self;
    
    self.advertiser = MCNearbyServiceAdvertiser(peer: self.peerId, discoveryInfo: nil, serviceType: "stc-classroom-vik");
    self.advertiser!.delegate = self;
}

and this is the crash/error I am getting:

2014-08-15 12:24:42.689 Xavier[614:254319] *** Terminating app due to uncaught exception     'NSInvalidArgumentException', reason: 'Invalid serviceType passed to MCNearbyServiceBrowser'

I would really appreciate any help.


Solution

  • I believe you're only allowed to have one hyphen in your serviceType parameter string, and it needs to be 15 characters or fewer. Yours has two hyphens and 17 characters.

    From the comments for MCNearbyServiceBrowser():

    The serviceType parameter is a short text string used to describe the app's networking protocol. It should be in the same format as a Bonjour service type: up to 15 characters long and valid characters include ASCII lowercase letters, numbers, and the hyphen. A short name that distinguishes itself from unrelated services is recommended; for example, a text chat app made by ABC company could use the service type "abc-txtchat".