当前位置 : 主页 > 手机开发 > ios >

ios – UIDocumentInteractionController()swift

来源:互联网 收集:自由互联 发布时间:2021-06-11
我创建了一个文件,我想通过UIDOcumentInteractionController分享它 我不确定如何从我保存文件的documentsPath和目标路径获取url let someText = NSString(string: "Test") let documentsPath = NSSearchPathForDirectorie
我创建了一个文件,我想通过UIDOcumentInteractionController分享它

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

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连接到故事板中的按钮.下一个:

>单击左侧栏中的蓝色项目图标,然后选择
水平菜单中的信息.
>展开文档类型,然后在名称字段中输入“txt”
“类型”字段中的“public.data,public.content”
>现在展开导出的UTI并在描述中输入“txt”
字段,标识符字段中的“kUTTypeText”和
Conforms to字段中的“public.data,public.content”

所以一切都是这样的:

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

网友评论