Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top