Search code examples
iosmultipeer-connectivitypeer

Objective C - Send Object with Multipeer Connectivity Framework, Peer not connected


I'm developing an application and I need to send an object to another application. These two apps communicate each other using the multipeer connectivity framework.

In particular, I want to send an object called Order, that is

@interface Order : NSObject <NSCoding>

@property (strong, nonatomic) NSString *customerName;
@property (strong, nonatomic) NSDate *arrivalTime;
@property (strong, nonatomic) NSNumber *totalPrice;
@property (strong, nonatomic) NSArray *products;

@end

from a "client" application called OrderAndPay to a "server" app called POS. Here is how I send this object in my OrderAndPay app delegate

NSData *dataToBeSent = [NSKeyedArchiver archivedDataWithRootObject:self.order];
NSError *error = nil;

NSArray *array = [[NSArray alloc] initWithObjects:[self.mpHandler.session connectedPeers], nil];

if ([self.mpHandler.session sendData:dataToBeSent
                            toPeers: array
                           withMode:MCSessionSendDataUnreliable
                              error:&error]) {
    return YES;
}

return NO;

But I always got NO as a result!!! When I try to debug, xCode say

Printing description of error: Error Domain=MCSession Code=1 "Peers ( ( POS ) ) not connected" UserInfo=0x16e8bcb0 {NSLocalizedDescription=Peers ( ( POS ) ) not connected}

So it seems that POS is not connected. But as I wrote before, I get my peers using the method connectedPeers of my MCSession!!!

How can I solve it?


Solution

  • Solved. I've changed the call in the if statement in

    [self.mpHandler.session sendData:dataToBeSent
                                toPeers: [self.mpHandler.session connectedPeers]
                               withMode:MCSessionSendDataUnreliable
                                  error:&error]
    

    and it worked.