xcode Swift:如何将本地 pdf 从我的应用程序打开到 iBooks
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27959023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift: How to Open local pdf from my app to iBooks
提问by devkw
I was in objective-c before. and this code below in objective C is working fine:
我之前在objective-c。下面这段代码在目标 C 中运行良好:
in. h
在.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 打开,这是我的 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. can anyone help me!
但是当我选择 iBooks 打开 pdf 时它会崩溃。谁能帮我!
回答by Nate4436271
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.
我认为 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.
}
}
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.
所以我只是在做一个演示项目来让它工作。就是这个。需要在函数外部声明它的原因是内存管理问题。一旦程序到达 buttonPressed 的末尾,它就不再知道 docController 是什么。然后,它尝试使用 docController 打开 iBooks,这是一个非持久变量,所以它崩溃了。
Hope this helps.
希望这可以帮助。
回答by Bojan Macele
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")
}
}
}
回答by Irrr Riiiiiii
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)
}
}