Multipeer Connectivity : Share Files to all peers simultaneously

1.5k views Asked by At

I'am working on a topic multipeer connectivity framework. I got a great idea about this framework using the below link :

http://www.appcoda.com/intro-multipeer-connectivity-framework-ios-programming/

But my problem is ,i can send chat messages to all connected peers and is received by them. But i need that same functionality in File sharing. I need to send files simultaneously to all connected peers. Is that possible????

2

There are 2 answers

2
Michał Ciuba On BEST ANSWER

Yes, it is possible. If you want to send large files (like tens of megabytes or more) I would recommend using method sendResourceAtURL:withName:toPeer:withCompletionHandler`` instead ofsendData:toPeers:withMode:error:`.

This way you don't have to load the whole file to memory at once (which may trigger a memory warning or even a crash). Also you get a NSProgress as a returned value, so you can show transfer's progress to the user.

NSURL* fileUrl = [NSURL fileURLWithPath:...];   //get the path of the file you'd like to send
    NSString* resourceName = @"<name to display>";
    for(MCPeerID *peer in session.connectedPeers) {
        [session sendResourceAtURL:fileUrl withName:resourceName toPeer:peer withCompletionHandler:^(NSError *error) {
            //handle transfer completion or error
        }];
    }
0
Naughty_Ottsel On

As long as you can convert the files to an NSData object, it appears it is possible.

In theory if you change this line:

NSData *dataToSend = [_txtMessage.text dataUsingEncoding:NSUTF8StringEncoding];

to:

NSData *dataToSend = [NSData dataWithContentsOfFile:@"Path to the file."];

and keep the rest the same it should still work.