Làm cách nào để thêm tệp PDF cho ứng dụng, nơi bạn nhấp vào nút để xem tệp & khi bạn hoàn tất, bạn quay lại màn hình bạn đang ở?Mở tệp PDF bằng swift
Trả lời
Nếu bạn chỉ muốn xem tệp PDF, bạn có thể tải tệp đó vào UIWebView.
let url : NSURL! = NSURL(string: "http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf")
webView.loadRequest(NSURLRequest(URL: url))
Nếu bạn muốn đạt được nhiều hơn, một khung tốt là PSPDFKit.
bạn có thể sử dụng UIDocumentInteractionController để xem trước tệp trong IOS. Trong file controller quan điểm của, thêm một loại tài sản của UIDocumentInteractionController
và thực hiện một phương pháp đại biểu đơn giản của nó
self.documentInteractionController = UIDocumentInteractionController.init(URL: url)
self.documentInteractionController?.delegate = self
self.documentInteractionController?.presentPreviewAnimated(true)
func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController {
return self
}
đừng quên để thêm UIDocumentInteractionControllerDelegate theo quan điểm điều khiển của lớp
self.documentInteractionController = UIDocumentInteractionController.init (URL: url) self.documentInteractionController? .delegate = self self.documentInteractionController? .presentPreviewAnimated (true) –
Đối với Xcode 8.1 và Swift 3.0
Lưu tệp PDF trong bất kỳ thư mục nào của xcode của bạn. Giả sử tên tập tin là 'Filename.pdf'
if let pdf = Bundle.main.url(forResource: "Filename", withExtension: "pdf", subdirectory: nil, localization: nil) {
let req = NSURLRequest(url: pdf)
yourWebViewOutletName.loadRequest(req as URLRequest)
}
Cùng sẽ được áp dụng nếu bạn muốn mở bất kỳ tập tin html.
Bạn cũng có thể sử dụng Khung nhìn nhanh từ Apple.
Rất linh hoạt.
Bạn có thể hiển thị tệp PDF của mình bằng tính năng thu phóng.
Bạn cũng có hỗ trợ cho tất cả các loại khác (như png, jpg, docx, txt, rtf, xlsx, zip, mov, vv) của tệp và rất dễ sử dụng.
Vui lòng tham khảo this câu trả lời nếu bạn muốn chi tiết mô tả của việc sử dụng QuickLook.framework
let openLink = NSURL(string : self.OtherContactorProfileVview.Result.CertificateList[index].CertificateFileLink)
if #available(iOS 9.0, *) {
let svc = SFSafariViewController(url: openLink! as URL)
present(svc, animated: true, completion: nil)
} else {
let port : PDFViewer = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PDFViewer") as! PDFViewer
port.strUrl = self.OtherContactorProfileVview.Result.CertificateList[index].CertificateFileLink
self.navigationController?.pushViewController(port, animated: true)
}
Đây không phải là câu trả lời tồi nếu ngữ cảnh thích hợp được thêm vào. Để sử dụng, bạn sẽ phải tạo một tệp PDFViewer nhanh và phù hợp với UIWebViewDelegate và dĩ nhiên self.OtherContactorProfileVview.Result.CertificateList [index] .CertificateFileLink là tên tệp pdf/tệp của bạn trong ví dụ trên – itsmcgh
SWIFT 4+
Nếu có thể mở tập tin từ bộ nhớ cache local/Documentdiectory có đường dẫn tệp
Phương pháp 1: sử dụng UIDocumentInteractionController
class ViewController: UIViewController,UIDocumentInteractionControllerDelegate {
let documentInteractionController = UIDocumentInteractionController(url: URL(fileURLWithPath: path))
documentInteractionController.delegate = self
documentInteractionController.presentPreview(animated: true)
}
//MARK: UIDocumentInteractionController delegates
func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
return self
}
Cách 2: sử dụng WebView
let webview = WKWebView(frame: UIScreen.main.bounds)
view.addSubview(webview)
webview.navigationDelegate = self
webview.load(URLRequest(url: URL(fileURLWithPath: path)))//URL(string: "http://") for web URL
- 1. Mở tệp pdf
- 2. Đính kèm tệp PDF vào email - Swift
- 3. JavaScript Phonegap mở tệp PDF
- 4. Mở tệp pdf từ máy chủ bằng ý định android
- 5. Mở PDF trong tab mới bằng dompdf
- 6. Cách mở tệp pdf từ Vim bằng ứng dụng pdf mặc định?
- 7. Tạo PDF với Swift
- 8. pdftk Lỗi: Không thể mở tệp PDF:
- 9. Tạo tệp PDF bằng JavaScript
- 10. Python: Không mở tệp bằng os.system()
- 11. iOS Swift tải lên tệp PDF với Alamofire (Multipart)
- 12. Xem tệp PDF bằng HTML không tải xuống tệp
- 13. Cách gửi tệp PDF bằng UIActivityViewController
- 14. mở khóa các tệp pdf được bảo vệ
- 15. Mở tệp PDF ngay lập tức từ ứng dụng Java
- 16. Cách mở tệp PDF cục bộ trong PhoneGap (android)
- 17. Cách mở tệp PDF cũng là tài nguyên dự án?
- 18. Cách tạo tệp PDF bằng Python
- 19. Dịch tệp PDF bằng Google Translate API
- 20. Cách tải xuống tệp PDF bằng Jersey?
- 21. Cách tạo tệp PDF bằng PHP?
- 22. Đọc Tệp PDF bằng iText5 cho .NET
- 23. Mở nhiều tài liệu PDF bằng cách sử batch file
- 24. Cách mở tệp PDF qua Intent từ thẻ SD
- 25. Mở tệp bằng inode
- 26. Cách mở PDF trong Android
- 27. Có thể mở tệp PDF bằng Preview.app tại một trang nhất định không?
- 28. Mở tệp pdf trực tiếp trong trình duyệt của tôi
- 29. Cách mở tệp pdf trong Windows Phone 8?
- 30. Làm thế nào để mở tệp PDF bên trong Chromium?
Cảm ơn câu trả lời! Nhưng điều này chỉ có thể được sử dụng từ một pdf bên ngoài, nó hoạt động nếu tôi muốn chèn pdf trong chương trình chính nó? – user3706773
UIWebView có thể tải một tệp PDF cục bộ. Thay vào đó hãy sử dụng '[NSURL fileURLWithPath: strURLPath];'. .đường dẫn sẽ là thư mục tài liệu của bạn hoặc gói tài nguyên của bạn. . Ngoài ra còn có một phương thức trên UIWebView để tải dữ liệu, vì vậy bạn có thể cho nó một đối tượng NSData. (Streaming từ tập tin có lẽ là tốt hơn cho việc sử dụng bộ nhớ mặc dù). –
Nếu trong thư mục tài nguyên: http://stackoverflow.com/questions/6398937/getting-a-list-of-files-in-the-resources-folder-ios (câu chuyện tương tự cho thư mục tài liệu). . . xây dựng NSURL từ chuỗi. –