Open PDF Documents in swift

使用下面的範例,就可以開啟  PDF 來 preview 也可以 OpenIn 到其他 app 裡。

Obj-C 的範例:

Swift 範例:

class ViewController: UIViewController, UIDocumentInteractionControllerDelegate {

    var docController:UIDocumentInteractionController!

    override func viewDidLoad() {
        // Do any additional setup after loading the view, typically from a nib.

        if let fileURL = NSBundle.mainBundle().pathForResource("SamplePDF1", ofType: "pdf") { // Use if let to unwrap to fileURL variable if file exists
            docController = UIDocumentInteractionController(URL: NSURL(fileURLWithPath: fileURL))

   = NSURL(fileURLWithPath: fileURL).lastPathComponent

            docController.delegate = self



    func documentInteractionControllerViewControllerForPreview(controller: UIDocumentInteractionController) -> UIViewController {
        return self
     func documentInteractionControllerDidEndPreview(controller: UIDocumentInteractionController) {
        docController = nil


發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *