|
func savePdf(urlString:String, fileName:String) { |
|
DispatchQueue.main.async { |
|
let url = URL(string: urlString) |
|
let pdfData = try? Data.init(contentsOf: url!) |
|
|
|
let resourceDocPath = URL(fileURLWithPath: NSTemporaryDirectory()) |
|
let pdfNameFromUrl = "YourAppName-\(fileName).pdf" |
|
let actualPath = resourceDocPath.appendingPathComponent(pdfNameFromUrl) |
|
do { |
|
try pdfData?.write(to: actualPath, options: .atomic) |
|
let activityController = UIActivityViewController(activityItems: [actualPath], applicationActivities: nil) |
|
DispatchQueue.main.async { |
|
self.showActivity(activity: activityController) |
|
} |
|
|
|
print("pdf successfully saved!") |
|
} catch { |
|
print("Pdf could not be saved") |
|
} |
|
} |
|
} |
|
|
|
func showSavedPdf(url:String, fileName:String) { |
|
if #available(iOS 10.0, *) { |
|
do { |
|
let docURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) |
|
let contents = try FileManager.default.contentsOfDirectory(at: docURL, includingPropertiesForKeys: [.fileResourceTypeKey], options: .skipsHiddenFiles) |
|
for url in contents { |
|
if url.description.contains("\(fileName).pdf") { |
|
// its your file! do what you want with it! |
|
|
|
} |
|
} |
|
} catch { |
|
print("could not locate pdf file !!!!!!!") |
|
} |
|
} |
|
} |
|
|
|
// check to avoid saving a file multiple times |
|
func pdfFileAlreadySaved(url:String, fileName:String)-> Bool { |
|
var status = false |
|
if #available(iOS 10.0, *) { |
|
do { |
|
let docURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) |
|
let contents = try FileManager.default.contentsOfDirectory(at: docURL, includingPropertiesForKeys: [.fileResourceTypeKey], options: .skipsHiddenFiles) |
|
for url in contents { |
|
if url.description.contains("YourAppName-\(fileName).pdf") { |
|
status = true |
|
} |
|
} |
|
} catch { |
|
print("could not locate pdf file !!!!!!!") |
|
} |
|
} |
|
return status |
|
} |