In Swift 4.1
// Url in String format
let urlStr = "http://www.msy.com.au/Parts/PARTS.pdf"
// Converting string to URL Object
let url = URL(string: urlStr)
// Get the PDF Data form the Url in a Data Object
let pdfData = try? Data.init(contentsOf: url!)
// Get the Document Directory path of the Application
let resourceDocPath = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
// Split the url into a string Array by separator "/" to get the pdf name
let pdfNameFromUrlArr = urlStr.components(separatedBy: "/")
// Appending the Document Directory path with the pdf name
let actualPath = resourceDocPath.appendingPathComponent(pdfNameFromUrlArr[
pdfNameFromUrlArr.count - 1])
// Writing the PDF file data to the Document Directory Path
do {
_ = try pdfData.write(to: actualPath, options: .atomic)
}catch{
print("Pdf can't be saved")
}
// Showing the pdf file name in a label
lblPdfName.text = pdfNameFromUrlArr[pdfNameFromUrlArr.count - 1]
// URLRequest for the PDF file saved in the Document Directory folder
let urlRequest = URLRequest(url: actualPath)
webVw.isUserInteractionEnabled = true
webVw.delegate = self
webVw.loadRequest(urlRequest)
Если вы хотите сохранить PDF-файлы в определенной папке / каталоге внутри Главного каталога документов
let resourceDocPath = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last! as URL
// The New Directory/folder name
let newPath = resourceDocPath.appendingPathComponent("QMSDocuments")
// Creating the New Directory inside Documents Directory
do {
try FileManager.default.createDirectory(atPath: newPath.path, withIntermediateDirectories: true, attributes: nil)
} catch let error as NSError {
NSLog("Unable to create directory \(error.debugDescription)")
}
// Split the url into a string Array by separator "/" to get the pdf name
pdfNameFromUrlArr = urlStr.components(separatedBy: "/")
// Appending to the newly created directory path with the pdf name
actualPath = newPath.appendingPathComponent(pdfNameFromUrlArr[pdfNameFromUrlArr.count - 1])
Happy Coding:)