Swift: How to Open local pdf from my app to iBooks

后端 未结 3 1789
轻奢々
轻奢々 2021-01-05 08:52

I was in objective-c before. and this code below in objective C is working fine:

in. h

@property (retain)UIDocumentInteractionController *docControll         


        
相关标签:
3条回答
  • 2021-01-05 09:28

    I think Bojan Macele had a great answer, and I used his code, I just think it needed some explanation. I had some trouble with the app crashing as well. Just make sure that docController is declared outside of the function you want to use it in. I have this variable declared right under my class declaration.

    import UIKit
    
    class ViewController: UIViewController {
    
        var button : UIButton?
        var docController: UIDocumentInteractionController?
    
        override func viewDidLoad() {
            super.viewDidLoad()
            button = UIButton(frame: CGRectMake(10, 50, 100, 50))
            button?.backgroundColor = UIColor.blackColor()
            self.view.addSubview(button!)
            button?.addTarget(self, action: "buttonPressed:", forControlEvents: UIControlEvents.TouchDown)
    
        }
    
        func buttonPressed(sender: UIButton){
            println("button pressed")
    
    
            if let path = NSBundle.mainBundle().pathForResource("test", ofType: "pdf") {
                if let targetURL = NSURL.fileURLWithPath(path) {
                    docController = UIDocumentInteractionController(URL: targetURL)
                    let url = NSURL(string:"itms-books:");
                    if UIApplication.sharedApplication().canOpenURL(url!) {
                        docController!.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)
                        println("iBooks is installed")
                    }else{
                        println("iBooks is not installed")
                    }
    
                }
            }
        }
    
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }
    }
    

    So I was just doing a demo project to get this working. This is it. The reason it needs to be declared outside of the function is for memory management issues. Once the program gets to the end of the buttonPressed, it no longer knows what docController is. Then, it tries to open iBooks using docController, which is a non persisting variable, so it crashes.

    Hope this helps.

    0 讨论(0)
  • 2021-01-05 09:32

    try this:

    var docController: UIDocumentInteractionController?
    
    if let path = NSBundle.mainBundle().pathForResource("book", ofType: "pdf") {
        if let targetURL = NSURL.fileURLWithPath(path) {
    
            docController = UIDocumentInteractionController(URL: targetURL)
            let url = NSURL(string:"itms-books:");
    
            if UIApplication.sharedApplication().canOpenURL(url!) {
    
                docController!.presentOpenInMenuFromRect(CGRectZero, inView: self.view, animated: true)
    
                println("iBooks is installed")
    
                }else{
    
                println("iBooks is not installed")
            }
    
        }
    }
    
    0 讨论(0)
  • 2021-01-05 09:38

    Swift 3

    var docController: UIDocumentInteractionController?
    
    if let path = Bundle.main.path(forResource: "book", ofType: "pdf") {
            let targetURL = NSURL.fileURL(withPath: path)
    
            docController = UIDocumentInteractionController(url: targetURL)
    
            let url = NSURL(string:"itms-books:")
    
            if UIApplication.shared.canOpenURL(url! as URL) {
    
                docController!.presentOpenInMenu(from: CGRect.zero, in: self.view, animated: true)
            }
        }
    
    0 讨论(0)
提交回复
热议问题