UIDocumentInteractionController()swift

我创建了一个文件,我想通过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连接到故事板中的按钮。 下一个:

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

所以一切都是这样的:

在此处输入图像描述

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