[英]How to browse pdf file from my iphone for my app, just like I am able to browse pdf file through ibooks?
就像使用UIImagePickerViewController
瀏覽UIIMage一樣。 有什么辦法可以瀏覽Swift /目標代碼中的另一個文件?
if let fileURL = NSBundle.mainBundle().URLForResource("MyImage", withExtension: "jpg") {
// Instantiate the interaction controller
self.docController = UIDocumentInteractionController(URL: fileURL)
}
else {
shareButton.enabled = false
print("File missing! Button has been disabled")
}
您只能訪問文檔文件夾中的文件。 訪問所有文件
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask)[0]
do {
let fileURLs = try fileManager.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil)
// process files
} catch {
print("Error while enumerating files \(documentsURL.path): \(error.localizedDescription)")
}
如果要單個文件,請查看以下代碼:
// Get the document directory url
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
do {
// Get the directory contents urls (including subfolders urls)
let directoryContents = try FileManager.default.contentsOfDirectory(at: documentsUrl, includingPropertiesForKeys: nil, options: [])
print(directoryContents)
// if you want to filter the directory contents you can do like this:
let mp3Files = directoryContents.filter{ $0.pathExtension == "mp3" }
print("mp3 urls:",mp3Files)
let mp3FileNames = mp3Files.map{ $0.deletingPathExtension().lastPathComponent }
print("mp3 list:", mp3FileNames)
} catch {
print(error.localizedDescription)
}
如果要在應用程序中查看PDF文件,可以采用兩種方法。
PDFView-可用的iOS 11.0
let pdfDocument = PDFDocument(url: url)
pdfView.document = pdfDocument
WKWebView-可用的iOS 8.0
webView.loadRequest(URLRequest.init(url: url))
您也可以使用UIWebview獲得相同的結果,但是已不建議使用。
或者,使用文件瀏覽器庫,您可以根據自己的需要進行自定義。 像這樣
https://developer.apple.com/documentation/pdfkit/pdfview https://developer.apple.com/documentation/webkit/wkwebview
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.