我创建了一个文件,我想通过以下方式共享它UIDocumentInteractionController
.
我不确定如何从documentsPath
和我保存文件的目标路径
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 连接到故事板中的一个按钮。下一个:
- 单击左侧边栏中的蓝色项目图标,然后选择Info从水平菜单。
- expand 文件类型并在其中输入“txt”Name场和
“公共数据,公共内容”中Types field
- 现在扩大输出性尿路感染并在其中输入“txt”描述字段中的“kUTTypeText”标识符场和
“公共数据,公共内容”中符合现场
让一切看起来像这样:
您现在可以构建并运行应用程序进行测试。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)