2

I am using Apple MultipeerConnectivity framework and I am able to send the message to all the peers connected to it.

I am using:

- (BOOL)sendData:(NSData *)data toPeers:(NSArray *)peerIDs withMode:(MCSessionSendDataMode)mode error:(NSError **)error;

I am even able to send the image by converting it in NSData.

Problem is I am trying to send image and text together to other peers. I know there are three ways to send information to other peers:

1. NSData

2. Resource

3. Stream

So for sending an image and text together which method we should use and how?

Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Paragon
  • 982
  • 2
  • 10
  • 37

1 Answers1

2

You could use NSData for your task. Here is example:
Serialization to NSData:

NSString *string = @"Any String";
UIImage *image = [UIImage imageNamed:@"Any Image"];
NSData *imageData = UIImagePNGRepresentation(image);
NSDictionary *dictionary = @{@"string":string, @"imageData":imageData};
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:dictionary];
// Send |data| by MultipeerConnectivity

Deserialization to NSString and UIImage:

// Receive |data| by MultipeerConnectivity
NSDictionary *dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSString *string = dictionary[@"string"];
NSData *imageData = dictionary[@"imageData"];
UIImage *image = [UIImage imageWithData:imageData scale:[UIScreen mainScreen].scale];
Vlad Papko
  • 13,184
  • 4
  • 41
  • 57
  • Thanks i was also doing the same. Just found out here. http://stackoverflow.com/questions/5513075/how-can-i-convert-nsdictionary-to-nsdata-and-vice-versa – Paragon Apr 16 '14 at 21:24