本文介绍了UIDocumentInteractionController()迅速的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个文件,并希望通过 UIDocumentInteractionController 共享它.

I created a file and I want to share it via UIDocumentInteractionController.

我不确定如何从 documentsPath 和保存文件的目标路径获取URL

I am unsure on how to obtain the URL from the documentsPath and destination path where I saved my file

        let someText = NSString(string: "Test")
        let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String

        let destinationPath = documentsPath.stringByAppendingPathComponent("Data.txt")
        var error:NSError?

        let written = someText.writeToFile(destinationPath,
            atomically: true,
            encoding: NSUTF8StringEncoding,
            error: &error)

        if written{
            println("Successfully stored the file at path \(destinationPath)")

  let dic = UIDocumentInteractionController()

   self.dic.URL = url
            let v = sender as UIView
            let ok = self.dic.presentOpenInMenuFromRect(
                v.bounds, inView: v, animated: true)

推荐答案

将代码修改为以下内容

import UIKit

class ViewController: UIViewController {
    var docController:UIDocumentInteractionController!

    override func viewDidLoad() {
        let someText = NSString(string: "Test")
        if let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as? String {

        let destinationPath = documentsPath.stringByAppendingPathComponent("Data.txt")
        var error:NSError?

        let written = someText.writeToFile(destinationPath,
            atomically: true,
            encoding: NSUTF8StringEncoding,
            error: &error)

        if written{
            println("Successfully stored the file at path \(destinationPath)")
            }
       if let url = NSURL(fileURLWithPath: destinationPath) {
            docController = UIDocumentInteractionController(URL: url)
        }
        }
    }

    @IBAction func sendFile(sender:AnyObject) {
    docController.presentOptionsMenuFromRect(sender.frame, inView:self.view, animated:true)
    }

}

现在将IBAction连接到情节提要中的按钮.下一个:

Now wire up the IBAction to a button in the storyboard. Next:

  1. 单击左侧栏中的蓝色项目图标,然后选择从水平菜单中信息.
  2. 扩展文档类型,然后在名称字段中输入"txt",然后类型字段
  3. 中的"public.data,public.content"
  4. 现在展开已导出的UTI ,然后在说明中输入"txt"字段,标识符字段中的"kUTTypeText"和符合字段
  5. 中的"public.data,public.content"
  1. click on the blue project icon in the left sidebar and then selectInfo from the horizontal menu.
  2. expand Document Types and enter "txt" in the Name field and"public.data, public.content" in the Types field
  3. now expand Exported UTIs and enter "txt" in the Descriptionfield, "kUTTypeText" in the Identifier field and"public.data, public.content" in the Conforms to field

所以一切看起来像这样:

So that everything looks like this:

您现在可以构建并运行该应用进行测试.

You can now build and run the app to test.

这篇关于UIDocumentInteractionController()迅速的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 23:33