2014-11-12 13 views
20

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

41

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.

+0

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

+2

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ù). –

+0

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. –

9

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(t‌​rue) 

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

+1

self.documentInteractionController = UIDocumentInteractionController.init (URL: url) self.documentInteractionController? .delegate = self self.documentInteractionController? .presentPreviewAnimated (true) –

13

Đố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.

2

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

0
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) 

     } 
+0

Đâ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

4

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 
Các vấn đề liên quan