0
votes

i am developing an application in which i will be sending a string to peer(after converting it into NSData object) and also an UIImage again by converting into NSData object.

Now at receiving end i have a receive method

- (void) receiveData:(NSData *)data
            fromPeer:(NSString *)peer
           inSession:(GKSession *)session
             context:(void *)context

My problem is that,same receive method is called every time i send any data,and i have to use image and text data differently.

So how can i know,whether i data that i have received is a NSString object converted into NSData or it's UIImage data converted into NSData.

please help me.

3

3 Answers

2
votes

Take a look at Apple's GKRocket sample code, specifically the implementation of sendData:ofType and receiveData:fromPeer:inSession:context: in SessionManager.m. You will need to define a set of "types" specific to your application and send them as a header in the NSData payload, and then read the header on the receiving end to determine how to handle the payload appropriately.

1
votes

The easiest way is to define another parameter (ofType) in recieveData that distinguishes what type of data is being sent:

(void) receiveData:(NSData *)data ofType: (bool type) fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context 
0
votes

The context parameter is supposed to be arbitrary data sent to provide a context or meaning for the sent data. It can be anything converted to data, even complex objects.

In this case a simple bool value would do.

The above is incorrect. The context is set by the receiver to distinguish between multiple sessions or peers. -- TechZen