简体   繁体   中英

Send PDF's using Multipeer Connectivity in swift

I have found an implementation of sending PDF's using Multipeer Connectivity in Objective C at the following link.

Multipeer Connectivity : Share Files to all peers simultaneously

I was wondering if anyone could provided a swift implementation for sharing PDFs via Multipeer Connectivity?

In my implementation, I am trying to send the files via the send(data: Data, toPeers: [MCPeerID], with: MCSessionSendDataMode) method. Is it possible to type cast the PDF to type Data ?

So after a week I have figured out that it is possible to represent a pdf as type data by using the dataRepresentation() method. Using this and the send(data: Data, toPeers: [MCPeerID], with: MCSessionSendDataMode) method, PDF's can be sent using multiplier connectivity.

Example Code

import PDFKit
import MultipeerConnectivity

let pdfDocument: PDFDocument? = PDFDocument(url: yourURLToPDFDocument)
let session: MCSession = MCSession(peer: yourPeerID, securityIdentity: nil, encryptionPreference: .required)

if let pdfData = pdfDocument?.dataRepresentation() {
    try session.send(pdfData, toPeers: yourArrayOfPeers, with: .reliable)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM