xcode 快速打开文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38549494/
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
Open with file in swift
提问by A.Munzer
When you open url that contain PDF file safari ask you if you want to open it on safari or in iBook.
当您打开包含 PDF 文件 safari 的 url 时,询问您是要在 safari 还是在 iBook 中打开它。
I want to do the same thing , in my project i had a collection view contains videos and photos, i want the user to chose if he want to open the file on the app or to open it with other media player.
我想做同样的事情,在我的项目中,我有一个包含视频和照片的集合视图,我希望用户选择是要在应用程序上打开文件还是使用其他媒体播放器打开它。
回答by sketchyTech
For loading into your own app it depends on which class you're using to display content on the exact code you'd use but for opening in another app you'd normally use a share button. Here is example code that will work if you wire up the @IBActionand @IBOutletto the same bar button in your UI (and place a file at the fileURL that you specify):
要加载到您自己的应用程序中,这取决于您使用哪个类在您将使用的确切代码上显示内容,但要在另一个应用程序中打开,您通常会使用共享按钮。下面是示例代码,如果您将@IBAction和@IBOutlet 连接到 UI 中的同一个栏按钮(并将文件放置在您指定的 fileURL 中):
import UIKit
class ViewController: UIViewController {
// UIDocumentInteractionController instance is a class property
var docController:UIDocumentInteractionController!
@IBOutlet weak var shareButton: UIBarButtonItem!
// called when bar button item is pressed
@IBAction func shareDoc(sender: AnyObject) {
// present UIDocumentInteractionController
if let barButton = sender as? UIBarButtonItem {
docController.presentOptionsMenuFromBarButtonItem(barButton, animated: true)
}
else {
print("Wrong button type, check that it is a UIBarButton")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// retrieve URL to file in main bundle
if let fileURL = NSBundle.mainBundle().URLForResource("MyImage", withExtension: "jpg") {
// Instantiate the interaction controller
self.docController = UIDocumentInteractionController(URL: fileURL)
}
else {
shareButton.enabled = false
print("File missing! Button has been disabled")
}
}
}
Notes
笔记
A UIDocumentInteractionControlleris used to enable the sharing of documents between your app and other apps installed on a user's device. It is simple to set up as long as you remember three rules:
一个UIDocumentInteractionController用来使您的应用,并安装在用户的设备上的其他应用程序之间共享文件。只要记住三个规则,设置就很简单:
Always make the UIDocumentInteractionController instance a class (type) property. If you only retain a reference to the controller for the life of the method that is triggered by the button press your app will crash.
Configure the UIDocumentInteractionController before the button calling the method is pressed so that there is not a wait in which the app is waiting for the popover to appear. This is important because while the presentation of the controller happens asynchronously, the instantiation does not. And you may find that there is a noticeable delay to open the popover if you throw all the code for instantiation and presentation inside a single method called on the press of a button. (When testing you might see a delay anyway because the share button is likely going to be pressed almost straightaway but in real world use there should be more time for the controller to prepare itself and so the possibility of lag is less likely.)
The third rule is that you musttest this on a real device not in the simulator.
始终使 UIDocumentInteractionController 实例成为类(类型)属性。如果您只在按钮触发的方法的生命周期内保留对控制器的引用,则您的应用程序将崩溃。
在按下调用该方法的按钮之前配置 UIDocumentInteractionController,以便应用程序不会等待弹出窗口出现。这很重要,因为虽然控制器的呈现是异步发生的,但实例化不是。并且您可能会发现,如果您将所有用于实例化和演示的代码都放在一个按按钮调用的单个方法中,则打开弹出窗口会有明显的延迟。(在测试时,无论如何您可能会看到延迟,因为共享按钮可能会几乎立即被按下,但在实际使用中,控制器应该有更多时间进行自我准备,因此延迟的可能性较小。)
第三条规则是您必须在真实设备上而不是在模拟器上进行测试。
More can be found in my blogposton the subject.
可以在我关于该主题的博文中找到更多信息。
Edit: Using a UIActivityViewController
编辑:使用 UIActivityViewController
Code for using UIActivityViewController instead of UIDocumentInteractionController
使用 UIActivityViewController 代替 UIDocumentInteractionController 的代码
import UIKit
class ViewController: UIViewController {
// UIDocumentInteractionController instance is a class property
var activityController: UIActivityViewController!
@IBOutlet weak var shareButton: UIBarButtonItem!
// called when bar button item is pressed
@IBAction func shareStuff(sender: AnyObject) {
if let barButton = sender as? UIBarButtonItem {
self.presentViewController(activityController, animated: true, completion: nil)
let presCon = activityController.popoverPresentationController
presCon?.barButtonItem = barButton
}
else {
print("not a bar button!")
}
}
override func viewDidLoad() {
super.viewDidLoad()
// retrieve URL to file in main bundle
if let img = UIImage(named:"MyImage.jpg") {
// Instantiate the interaction controller
activityController = UIActivityViewController(activityItems: [img], applicationActivities: nil)
}
else {
shareButton.enabled = false
print("file missing!")
}
}
}
You can also add custom activitiesto the UIActivityViewController and here is codefor adding an "Open In..." button to a UIActivityViewController so that you can switch to a UIDocumentInteractionController from a UIActivityViewController.
您还可以向 UIActivityViewController添加自定义活动,这里是向 UIActivityViewController添加“打开方式...”按钮的代码,以便您可以从 UIActivityViewController 切换到 UIDocumentInteractionController。
回答by emido333
I did the same code for saving a PDF file from a URL (whether it's a local URL in your device storage, or it's a URL from somewhere on the internet)
我做了相同的代码来从 URL 保存 PDF 文件(无论它是设备存储中的本地 URL,还是来自互联网上某个地方的 URL)
Here is the Code for Swift 3 :
这是 Swift 3 的代码:
@IBOutlet weak var pdfWebView: UIWebView!
@IBOutlet weak var shareBtnItem: UIBarButtonItem!
var pdfURL : URL!
var docController : UIDocumentInteractionController!
then in viewDidLoad()
然后在 viewDidLoad()
// retrieve URL to file in main bundle`
let fileURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0].appendingPathComponent("YOUR_FILE_NAME.pdf")
//Instantiate the interaction controller`
self.docController = UIDocumentInteractionController(url: fileURL)`
and in your barButtonItem tapped method (which I have called openIn(sender)):
在你的 barButtonItem 方法中(我称之为 openIn(sender)):
@IBAction func openIn(_ sender: UIBarButtonItem)
{
// present UIDocumentInteractionController`
docController.presentOptionsMenu(from: sender, animated: true)
}
FYI: You need a webView in your storyboard if you wish to show the pdf file as well
仅供参考:如果您还想显示 pdf 文件,则您的故事板中需要一个 webView
Hope this helps.
希望这可以帮助。