Ios UIDocumentInteractionController()swift
我创建了一个文件,我想通过Ios UIDocumentInteractionController()swift,ios,swift,Ios,Swift,我创建了一个文件,我想通过UIDocumentInteractionController共享它 我不确定如何从文档SPAH和保存文件的目标路径获取URL let someText = NSString(string: "Test") let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as St
UIDocumentInteractionController
共享它
我不确定如何从文档SPAH
和保存文件的目标路径获取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连接到故事板中的按钮。下一步: