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

标签 ios objective-c xcode swift

我之前在 objective-c。 Objective-C 中的以下代码运行良好:

在。

@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 代码:

      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 时它崩溃了。谁能帮帮我!

最佳答案

我认为 Bojan Macele 有一个很好的答案,我使用了他的代码,我只是认为它需要一些解释。我也遇到了应用程序崩溃的问题。只需确保在要使用它的函数之外声明了 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.
    }
}

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

希望这对您有所帮助。

关于ios - Swift:如何从我的应用程序打开本地 pdf 到 iBooks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27959023/

相关文章:

objective-c - Cookies/Cocoa WebView 状态未被保留

iphone - 在 iphone app/ios 中,如何制作覆盖导航栏和键盘的透明 'loading' 覆盖层?

ios - FMDB 没有显示任何内容 - swift

iOS 框架不适用于模拟器

ios - 应用程序进入后台后未调用 animationController For Operation

iphone - 在没有引用的情况下查找 UIAlertView

iphone - 为 iPad 设置图标,应用程序的目标设备是 iPhone

objective-c - 如何将 GPUImage 添加到 iOS 项目?

ios - Xcode 9 "could not build module ‘Foundation’“

objective-c - 为什么在尝试编译此代码时会出现错误 "error: unknown type name ' virtual'"?