Question

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

Was it helpful?

Solution

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top