问题
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????
回答1:
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 of
sendData: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
}];
}
回答2:
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.
来源:https://stackoverflow.com/questions/27184493/multipeer-connectivity-share-files-to-all-peers-simultaneously