簡體   English   中英

多對等連接:同時將文件共享給所有對等

[英]Multipeer Connectivity : Share Files to all peers simultaneously

我正在研究主題多方連接框架。 我使用以下鏈接對這個框架有了一個很好的主意:

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

但是我的問題是,我可以向所有連接的對等方發送聊天消息並被他們接收。 但是我在文件共享中需要相同的功能。 我需要同時將文件發送到所有連接的對等方。 那可能嗎????

對的,這是可能的。 如果要發送大文件(例如數十兆字節或更多),我建議使用sendResourceAtURL:withName:toPeer:withCompletionHandler`` instead of方法sendResourceAtURL:withName:toPeer:withCompletionHandler`` instead of sendData:toPeers:withMode:error:`方法。

這樣,您不必一次將整個文件加載到內存中(這可能會觸發內存警告甚至崩潰)。 另外,您NSProgress獲得NSProgress作為返回值,因此可以向用戶顯示傳輸進度。

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

只要您可以將文件轉換為NSData對象,就可以實現。

理論上,如果您更改此行:

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

至:

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

其余的保持不變,仍然可以正常使用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM