Swift:如何从我的应用程序向iBooks打开本地pdf [英] Swift: How to Open local pdf from my app to iBooks

查看:86
本文介绍了Swift:如何从我的应用程序向iBooks打开本地pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前在Objective-C中.并且下面目标C中的这段代码可以正常工作:

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

in. h

@property (retain)UIDocumentInteractionController *docController;

和.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");

}

但是现在我正在尝试使用swift打开,这是我的swift代码:

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

        }
    }

但是当我选择iBooks打开pdf时会崩溃.谁能帮我!

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

推荐答案

我认为Bojan Macele有一个很好的答案,我使用了他的代码,我只是认为它需要一些解释.我在应用崩溃时也遇到了一些麻烦.只需确保在要在其中使用它的函数之外声明了docController.我在类声明下声明了该变量.

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.
    }
}

所以我只是在做一个演示项目来使它工作.就是这个.需要在函数外部声明它的原因是出于内存管理问题.一旦程序到达buttonPressed的末尾,它就不再知道docController是什么.然后,它尝试使用docController打开iBooks,它是一个非持久变量,因此崩溃.

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.

希望这会有所帮助.

这篇关于Swift:如何从我的应用程序向iBooks打开本地pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆