Multipeer Connectivity : Share Files to all peers simultaneously

前提是你 提交于 2020-01-25 06:15:29

问题


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 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
        }];
    }



回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!