UIDocumentInteractionController()迅速

编程入门 行业动态 更新时间:2024-10-13 14:24:51
本文介绍了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:

  • 单击左侧栏中的蓝色项目图标,然后选择从水平菜单中信息.
  • 扩展文档类型,然后在名称字段中输入"txt",然后类型字段
  • 中的"public.data,public.content"
  • 现在展开已导出的UTI ,然后在说明中输入"txt"字段,标识符字段中的"kUTTypeText"和符合字段
  • 中的"public.data,public.content"
  • click on the blue project icon in the left sidebar and then select Info from the horizontal menu.
  • expand Document Types and enter "txt" in the Name field and "public.data, public.content" in the Types field
  • now expand Exported UTIs and enter "txt" in the Description field, "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()迅速

    本文发布于:2023-11-26 20:36:54,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1635106.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:UIDocumentInteractionController

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!