Bancha Rojkittisakul
Bancha Rojkittisakul

Reputation: 166

Send NSString via Game Center

I want to send NSString form another one to another one iPhone/iPad via Gamecenter but it crash with EXC_BAD_ACCESS

here in .h file

typedef enum {
    kMessageTypeRandomNumber = 0,
    kMessageTypeGameBegin,
    kMessageTypeSubmit,
    kMessageTypeExchange,
    kMessageTypePickup,
    kMessageTypePass,
    kMessageTypeGameOver
} MessageType;

typedef struct {
    MessageType messageType;
} Message;

typedef struct {
Message message;
NSString *submitTile;
} MessageSubmit;

and here in .m file

- (void)sendData:(NSData *)data {
    NSError *error;
    BOOL success = [[GCHelper sharedInstance].match sendDataToAllPlayers:data withDataMode:GKMatchSendDataReliable error:&error];
    if (!success) {
        CCLOG(@"Error sending init packet");
        [self matchEnded];
    }
}
-(void)sendSubmit:(NSString *) submitTile {
    MessageSubmit message;
    message.message.messageType = kMessageTypeSubmit;
    message.submitTile = submitTile;
    NSData *data = [NSData dataWithBytes:&message length:sizeof(MessageSubmit)];    
    [self sendData:data];
}

and the if I click on CCMenu image it will call onSubmit function and here are onSubmit function

-(void)onSubmit
{
    NSString *submitStr = @"1-7-7 =-7-8 1-7-9";

    [self sendSubmit:submitStr];
}

and the last one is didReceiveData method

- (void)match:(GKMatch *)match didReceiveData:(NSData *)data fromPlayer:(NSString *)playerID {
    if (message->messageType == kMessageTypeSubmit) {
        MessageSubmit * messageSubmit = (MessageSubmit *) [data bytes];
        NSString *submitStr = messageSubmit->submitTile;

        NSLog(@"SubTile %@",submitStr);
    }
}

it have EXC_BAD_ACCESS on line NSString *submitStr = messageSubmit->submitTile;.

Is there some way to send NSString message over iPhone/iPad?

Upvotes: 0

Views: 1032

Answers (2)

Kaan Dedeoglu
Kaan Dedeoglu

Reputation: 14841

try this:

To encode:

NSData *data = [stringToEncode dataUsingEncoding:NSUTF8Encoding];

which you can send using Game Center

To decode:

NSString *recievedString = [NSString stringWithUTF8String:[recievedData bytes]];

Edit: Now I think you can even put this data in one of your message structs and do the same [NSData dataWithBytes:&message length:sizeof(MessageSubmit)] trick and send it.

Upvotes: 1

Jim
Jim

Reputation: 73936

You can't do this:

NSData *data = [NSData dataWithBytes:&message length:sizeof(MessageSubmit)];    

...or this:

MessageSubmit * messageSubmit = (MessageSubmit *) [data bytes];

Generally speaking, you can't just send the in-memory representation of objects around as-is. For example, the submitTile instance variable of that class is a pointer to an NSString object. When you send the data over the network, you aren't sending the string itself, you are sending the pointer - which is just a memory address of a chunk of memory on the sending device. The receiving device won't have the same string stored anywhere, and it wouldn't have the same memory address even if it did. So you've got a nonsensical pointer pointing to nowhere, and you're expecting it to point to a string that doesn't exist.

The easiest way of doing what you want to do is to make your MessageSubmit class NSCoding-compliant. Serialise it into NSData instead of just making a copy of the bytes.

Upvotes: 1

Related Questions