0
votes

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?

2

2 Answers

0
votes

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

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.