views:

137

answers:

3

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.

+1  A: 

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 
ennuikiller
i could not get you.I am sending an NSData object in both cases.What you mean by dataofType?Can you please elaborate.because in any case i will receive same type of object.
Ajayvictor007
A: 
TechZen
NSError *er=[[NSError alloc] initWithDomain:@"anurag" code:123 userInfo:nil]; [mSession sendDataToAllPeers:myData withDataMode:GKSendDataReliable error:
Ajayvictor007
this is what i am using while sending the data.How can i use context in it?
Ajayvictor007
Sorry, my mistake, the `context` is set on by the receiver to be passed to its own delegate. It's apparently intended to distinguish between multiple sessions.
TechZen
+2  A: 

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.

Nathan de Vries
+1 This is the correct answer.
TechZen