Search code examples
iosswiftpdfmultipeer-connectivity

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?


Solution

  • 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)
    }