I'm trying to show a pdf in my iOS app using Swift. The response I get from the server is the pdf data and it's looking like this:
%PDF-1.4 %ÓôÌá 1 0 obj << /CreationDate(D:20180403085232+02'00') /Creator(PDFsharp 1.32.3057-g (www.pdfsharp.net)) /Producer(PDFsharp 1.32.3057-g (www.pdfsharp.net)) endobj 2 0 obj << /Type/Catalog /Pages 3 0 R endobj 3 0 obj << /Type/Pages /Count 3 /Kids[4 0 R 8 0 R 11 0 R] endobj 4 0 obj << /Type/Page /MediaBox[0 0 612 792] /Parent 3 0 R /Contents 5 0 R /Resources << /ProcSet [/PDF/Text/ImageB/ImageC/ImageI] /XObject << /I0 6 0 R /ExtGState << /GS0 7 0 R /Group << /CS/DeviceRGB /S/Transparency /I false /K false endobj 5 0 obj << /Length 260 /Filter/FlateDecode stream xœíRMK1½÷Wä˜M“&Mî¢àa]oâIEDF‚¿ÞÌ̺²ø¥”4y¯—´C©@¹ŽÆЃáú!ÏDpñZ†2€š W9#¾gNŽa“/…à4÷}©gåòŠàf§{¾.«õ–àWìÚ¡WÆT4Bµ †¡O·e[F>g^í=|UEw‡ª‚Ö$å(U‘ôÇ›„L¶ÜÑ™—~¤6•š'Ú9f”Pz›PŽ€¦¨"бv_gly›Û÷Mä~.î] ™Ûë؈?œå€«Ul¡À™y_6þ»Aÿ—ÿ¤|èYòÿX`ìfÈ»ï³ÉõŠ¹·ò endstream endobj 6 0 obj << /Type/XObject /Subtype/Image /Length 5097196 /Filter/FlateDecode /Width 2511 /Height 3531 /BitsPerComponent 8 /ColorSpace/DeviceRGB /Interpolate true stream xœìÝ œ¤g]'ðïé9rÂáˆJAE%€ Š"ë‚FQ0 Ë!º» ¢‚+^ (h
I checked iOS-PDF-Reader and PDFKit but the main problem is that I don't understand how I can handle the response in the app.
fileprivate func loadPdf() {
startIndication()
var session: URLSession!
let sessionConfig = URLSessionConfiguration.default
session = URLSession(configuration: sessionConfig, delegate: nil, delegateQueue: nil)
guard let URL = URL(string: "http://localhost:3000/pdf-download") else { return }
let token = "Bearer " + self.user!.token!
var request = URLRequest(url: URL)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue(token, forHTTPHeaderField: "Authorization")
var invData:[String:Any] = [String:Any]()
invData["INVID"] = invoice.ID
request.httpBody = try! JSONSerialization.data(withJSONObject: invData, options: [])
let task = session.dataTask(with: request) { (data:Data?, response: URLResponse?, error:Error?) in
if error == nil {
guard let data = data else { return }
// I get the data but I cannot do anything with it
} else {
print("response: \(String(describing: error))")
}
}
task.resume()
}
Any ideas? Thanks!