在swift中打开文件

A.M*_*zer 3 xcode ios swift

当您打开包含PDF文件safari的URL时,询问您是否要在safari或iBook中打开它.

我想做同样的事情,在我的项目中,我有一个包含视频和照片的集合视图,我希望用户选择是否要在应用程序上打开文件或用其他媒体播放器打开它.

ske*_*ech 6

要加载到您自己的应用程序中,它取决于您使用哪个类来显示您使用的确切代码的内容,但是在另一个应用程序中打开您通常使用共享按钮.下面是示例代码,如果您将@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")
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

笔记

一个UIDocumentInteractionController用来使您的应用,并安装在用户的设备上的其他应用程序之间共享文件.只要你记住三条规则,设置就很简单:

  1. 始终使UIDocumentInteractionController实例成为类(类型)属性.如果您仅在按钮触发的方法的生命周期内保留对控制器的引用,则您的应用程序将崩溃.

  2. 在按下调用方法的按钮之前配置UIDocumentInteractionController,以便没有等待应用程序等待弹出窗口出现的等待.这很重要,因为虽然控制器的表示是异步发生的,但实例化却没有.并且如果您将实例化和演示的所有代码都放在按下按钮的单个方法中,您可能会发现打开popover会有明显的延迟.(测试时你可能会看到一个延迟,因为分享按钮可能几乎是直接按下,但在实际使用中,控制器应该有更多时间做好准备,因此滞后的可能性较小.)

  3. 第三条规则是你必须在不在模拟器中的真实设备上测试它.

UIDocumentInteractionController图片 有关此主题的博文中可以找到更多内容.

编辑:使用UIActivityViewController

使用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!")
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

您还可以向UIActivityViewController 添加自定义活动,这里是用于向UIActivityViewController 添加"Open In ..."按钮的代码,以便您可以从UIActivityViewController切换到UIDocumentInteractionController.