2
votes

I'm making an iOS 7 app with the Multipeer connectivity framework, but I am unable to get two devices to recognize each other. I've looked through the documentation and wwdc video, and the information on this framework is very limited besides that. Does anyone have experience working with the new peer-to-peer capability and can help?

Here's basically what I have so far. The browserVC is presented on the screen but no devices are found when I'm running the app on two devices.

MCPeerID *peer = [[MCPeerID alloc] initWithDisplayName:@"user"];
  _session =  [[MCSession alloc] initWithPeer:peer];
  NSString *service = @"nsync";

  _session.delegate = self;

  MCAdvertiserAssistant *assistant =[[MCAdvertiserAssistant alloc] initWithServiceType:service
                                                                         discoveryInfo:nil
                                                                               session:_session];
  [assistant start];


  MCBrowserViewController *browserVC = [[MCBrowserViewController alloc] initWithServiceType:service
                                                                                    session:_session];
  browserVC.delegate = self;
  [self presentViewController:browserVC
                     animated:YES
                   completion:nil];
4
This question can't really be answered as-is, you may want to make the question more specific or ask the question in a different forum, such as the Stack Exchange chat or IRC. - Dietrich Epp
And the Apple Developer Forums are up again - so you should try there as well. - Abizern
Okay, thanks. I still can't access the Developer Forums but I'm guessing that I should be able to soon. - Kaitlyn Lee

4 Answers

2
votes

To allow your browser to see devices, you'll need to have some other devices that are advertising. I think your problem is that your MCAdvertiserAssistant is going out of scope and being deallocated, as it's only stored in a local variable.

Here is the code I've used to advertise:

#define SERVICE_TYPE @"MyServiceType"
...

@property (nonatomic, strong) MCAdvertiserAssistant* advertiserAssistant;
...

self.peerId = [[MCPeerID alloc] initWithDisplayName:[[UIDevice currentDevice] name]];
self.advertiserSession = [[MCSession alloc] initWithPeer:self.peerId];
self.advertiserSession.delegate = self;
self.advertiserAssistant = [[MCAdvertiserAssistant alloc] initWithServiceType:SERVICE_TYPE discoveryInfo:nil session:self.advertiserSession];
[self.advertiserAssistant start];

Note that I'm storing the advertiser assistant in a property, so that it will not be deallocated as soon as the method that creates it finishes.

And to browse:

self.peerId = [[MCPeerID alloc] initWithDisplayName:[[UIDevice currentDevice] name]];
self.browserSession = [[MCSession alloc] initWithPeer:self.peerId];
self.browserSession.delegate = self;
self.browser = [[MCBrowserViewController alloc] initWithServiceType:SERVICE_TYPE session:self.browserSession];
self.browser.delegate = self;
[self presentViewController:self.browser animated:YES completion:nil];
1
votes

do not declare MCAdvertiserAssistant *assistant as local variable, declare as a class member.

0
votes

I agree.

I just try this yesterday, it works like a charm. Your code seems to be correct except for your MCAdvertiserAssistant. It should be set as a global variable !

And as Greg said, you have to run your app on 2 devices that are connected at least with wifi or bluetooth (not internet connection required). Be aware that it won't work with cellular network.

0
votes

I agree. A syntax that worked for me (at least to get them to see one another; I'm still having trouble getting an invitation accepted... :) is:

@property   (strong, nonatomic) MCSession   *theSession;
@property   (strong, nonatomic) MCAdvertiserAssistant       *assistant;
@property   (strong, nonatomic) MCBrowserViewController     *browserVC;

and then later,

    UIDevice *thisDevice = [UIDevice currentDevice];

    MCPeerID *aPeerID = [[ MCPeerID alloc ] initWithDisplayName: thisDevice.name];
    self.theSession = [[ MCSession alloc ] initWithPeer: aPeerID ];
    self.theSession.delegate = self;

    self.assistant = [[MCAdvertiserAssistant alloc] initWithServiceType:kServiceType
                                                          discoveryInfo:nil
                                                                session:self.theSession ];
    self.assistant.delegate = self;
    [ self.assistant start ];

and

    self.browserVC = [[MCBrowserViewController alloc] initWithServiceType:kServiceType session:self.theSession];
    self.browserVC.delegate = self;
    [ self.window.rootViewController presentViewController:self.browserVC animated:YES completion:nil];

(had to use rootViewController because I was doing this not in my main VC.)