2
votes

I am using multipeer connectivity in ios7 in my app. The file sending and receiving works absolutely fine, but in the moment that a user accesses from within my app the control center (or even settings) and switches off either bluetooth or wifi, the file exchange stops working. When the user witches both of them back on, still it doesn't work. In order for them to work again, the user must close and re-open the app.

The files are sent in this way:

MCSession *session = [[MCSession alloc]
                                          initWithPeer:key];


                    NSCalendar *gregorian = [[NSCalendar alloc]
                                             initWithCalendarIdentifier:NSGregorianCalendar];

                    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
                    dateComponents.year = 2100;
                    dateComponents.month = 1;
                    dateComponents.day = 1;
                    dateComponents.hour = 0;
                    dateComponents.minute = 0;
                    dateComponents.second = 0;

                    NSDate *referenceDate = [gregorian dateFromComponents: dateComponents];

                    NSDate *now = [NSDate date];
                    NSTimeInterval interval = [now timeIntervalSinceDate:referenceDate];

                    NSData *Recording = [NSData dataWithContentsOfFile:myFilePath];


                    NSString* str = [NSString stringWithFormat:@"%@.ext", button.titleLabel.text];


                    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
                    [dict setObject:str forKey:@"fileName"];
                    [dict setObject:@"Recording" forKey:@"fileType"];
                    [dict setObject:Recording forKey:@"FileData"];

                    NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:dict];

                    [browser invitePeer:key
                              toSession:session
                            withContext:myData
                                timeout:interval];

The user can reload the devices at any time using:

[browser startBrowsingForPeers];

I think that the problem is the timeout, but I am not sure.

2
You need to re-init the session in the - (void)applicationDidBecomeActive:(UIApplication *)application app delegate method. That method is called when your app becomes active again after control center being opened and then closed. - Andrew
By re-init the session you mean just the MCSession or even the MCNearbyServiceAdvertiser and the MCNearbyServiceBrowser? - Alessandro
If you can, reinit everything that has to do with the Multipeer Connectivity Framework. You will need to reconnect all connected peers as well. - Andrew
ok, did it and the problem seems to be solved, but for some reason, not only all devices are re-appearing, but also the device itself. It seems like the device can send a file to itself... - Alessandro
That has happened to be before as well with a different framework. You can try to identify a peer as itself and not show it if you are listing peers. - Andrew

2 Answers

2
votes

You need to re-init all the Multipeer Connectivity Framework related instances in the - (void)applicationDidBecomeActive:(UIApplication *)application app delegate method. That method is called when your app becomes active again after control center being opened and then closed.

1
votes

I propose you instead monitor for changes to connectivity status, and teardown or re-init when you detect a change there. Here is my method for this same problem, using the excellent Reachability kit:

- (void)monitorReachability
{
    // Allocate a reachability object
    self.reachability = [Reachability reachabilityWithHostname:@"www.google.com"];

    [[NSNotificationCenter defaultCenter] addObserverForName:kReachabilityChangedNotification object:nil queue:nil usingBlock:^(NSNotification *note) {
        Reachability *reachability = note.object;
        if ( reachability.isReachable ) {
            self.isPhysicallyConnected = YES;
            [self updateNearbyService];
        } else {
            self.isPhysicallyConnected = NO;
            [self tearDownNearbyService];
        }
    }];

    // Start the notifier, which will cause the reachability object to retain itself!
    [self.reachability startNotifier];
}

In teardown and update you would reconstruct your Multipeer stack.