I’m attempting to obtain a PDF from an API name, and have it open robotically upon obtain within the PDF viewer (e.g. in Information). I don’t need it to be displayed in-app.
The file does obtain, and I can discover it in Information > On My iPhone > AppName
, nevertheless it would not open robotically. How can I obtain that?
func downloadContractAgreement() {
guard let url = URL(string: "[redacted]" else {
print("Invalid URL")
return
}
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("*/*", forHTTPHeaderField: "Settle for")
let keychain = Keychain()
var token = keychain["authToken"] ?? "none"
request.setValue("Bearer (token)", forHTTPHeaderField: "Authorization")
let job = URLSession.shared.downloadTask(with: request) { localURL, response, error in
if let localURL = localURL {
let destinationURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("downloaded2.pdf")
do {
attempt FileManager.default.moveItem(at: localURL, to: destinationURL)
DispatchQueue.foremost.async {
UIApplication.shared.open(destinationURL) { success in
if !success {
print("Did not open (destinationURL)")
}
}
}
} catch {
print("File error: (error)")
}
} else if let error = error {
print("Error: (error)")
}
}
job.resume()
}