Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

in. h

@property (retain)UIDocumentInteractionController *docController;

and in .m

    NSString *path = [[NSBundle mainBundle] pathForResource:@"book" ofType:@"pdf"];
NSURL *targetURL = [NSURL fileURLWithPath:path];


docController = [UIDocumentInteractionController interactionControllerWithURL:targetURL];

if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"itms-books:"]]) {

    [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
    NSLog(@"iBooks installed");

} else {

    NSLog(@"iBooks not installed");

}

but now i am trying to use swift to open and this is my swift code:

      if let path = NSBundle.mainBundle().pathForResource("book", ofType: "pdf") {
        if let targetURL = NSURL.fileURLWithPath(path) {

            let 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")
            }

        }
    }

but it crash when I select iBooks to open the pdf. can anyone help me!

like image 441
devkw Avatar asked Jan 15 '15 08:01

devkw


2 Answers

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.

like image 145
Nate4436271 Avatar answered Oct 30 '22 23:10

Nate4436271


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")
        }

    }
}
like image 42
Bojan Macele Avatar answered Oct 30 '22 22:10

Bojan Macele