gpt4 book ai didi

ios - UIDocumentInteractionController() swift

转载 作者:搜寻专家 更新时间:2023-10-31 22:03:22 25 4
gpt4 key购买 nike

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

  1. 单击左侧边栏中的蓝色项目图标,然后选择水平菜单中的信息
  2. 展开文档类型并在名称字段中输入“txt”,然后Types 字段中的“public.data, public.content”
  3. 现在展开Exported UTIs并在Description中输入“txt”字段,Identifier 字段中的“kUTTypeText”和符合字段
  4. 中的“public.data, public.content”

所以一切看起来像这样:

enter image description here

您现在可以构建并运行要测试的应用。

关于ios - UIDocumentInteractionController() swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29399341/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com