简体   繁体   中英

PDF file not open in UIDocumentInteractionController

I am working on the simulator any there are no apps like adobe to open the pdf files....i am using UIDocumentInteractionController to open the pdf file like below.

let url=URL(fileURLWithPath: "/Users/sai/Library/Developer/CoreSimulator/Devices/425D8615-ADEC-4334-A639-C30B1E675EFA/data/Containers/Data/Application/AB3787BF-F6AC-4111-9847-A0FDC4E899F8/tmp/samplepdf.pdf")

    print("--------------------------------- str is \(url)")
    if (url != nil) {
        // Initialize Document Interaction Controller
        self.documentInteractionController = UIDocumentInteractionController(url: url)
        // Configure Document Interaction Controller
        self.documentInteractionController?.delegate = self
        // Preview PDF
        self.documentInteractionController?.presentOptionsMenu(from: self.view.frame, in: self.view, animated: true)
    }


  func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
            return self
  }

My screen is getting displayed like below No pdf is being displayed here...is it because there is no app in my simulator which could open pdf files?

Not sure if any Simulator apps will handle pdf documents - maybe News? But you can just add a webView to a view controller and use that to view your pdf.

@IBOutlet weak var webView: UIWebView!

Do the usual safety checks on your url and then:

webView.loadRequest(NSURLRequest(url: url as URL) as URLRequest)

I think there is a path issue.

Replace

let url=URL(fileURLWithPath: "/Users/sai/Library/Developer/CoreSimulator/Devices/425D8615-ADEC-4334-A639-C30B1E675EFA/data/Containers/Data/Application/AB3787BF-F6AC-4111-9847-A0FDC4E899F8/tmp/samplepdf.pdf")

With

let url = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent("samplepdf.pdf")

for Swift 3/Xcode 8

let url = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent("samplepdf.pdf") as NSURL

Hope that will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM