简体   繁体   English

Swift:如何从我的应用程序打开本地pdf到iBooks

[英]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: 以下目标C中的代码工作正常:

in. h in。h

@property (retain)UIDocumentInteractionController *docController;

and in .m 在.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: 但现在我正在尝试使用swift打开,这是我的快速代码:

      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. 但是当我选择iBooks打开pdf时会崩溃。 can anyone help me! 谁能帮我!

I think Bojan Macele had a great answer, and I used his code, I just think it needed some explanation. 我认为Bojan Macele有一个很好的答案,我使用他的代码,我认为它需要一些解释。 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. 只要确保docController是在你想要使用它的函数之外声明的。我在我的类声明下声明了这个变量。

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. 一旦程序到达buttonPressed的末尾,它就不再知道docController是什么。 Then, it tries to open iBooks using docController, which is a non persisting variable, so it crashes. 然后,它尝试使用docController打开iBooks,这是一个非持久性变量,因此它崩溃了。

Hope this helps. 希望这可以帮助。

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

    }
}

Swift 3 斯威夫特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)
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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