-1

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?

dylaniato
  • 516
  • 9
  • 23

2 Answers2

1

you have to give same sessionType in both applications.. static NSString * const kMCSessionServiceType = @"mcsessionp2p";

 _serviceAdvertiser = [[MCNearbyServiceAdvertiser alloc] initWithPeer:self.peerID
                                                       discoveryInfo:nil
                                                         serviceType:kMCSessionServiceType];


// Create the service browser
_serviceBrowser = [[MCNearbyServiceBrowser alloc] initWithPeer:self.peerID
                                                   serviceType:kMCSessionServiceType
0

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.

dylaniato
  • 516
  • 9
  • 23