我正在尝试使用iOS的共享功能分享音频文件。我无法将音频文件音频文件放入共享表进行共享。目前,没有添加到电子邮件中的附件或音频文件(例如)。
我有以下代码来检索音频文件。我可以使用MFMailComposeViewController
明确地将其作为电子邮件发送,但是却希望在共享表中“抛出”它以允许用户上传到电子邮件/ dropbox / google驱动器文件。
这可能吗?
let objectsToShare: NSMutableArray = []
let docsDir = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let soundFilePath = (docsDir as NSString).stringByAppendingPathComponent(fileName)
let fileManager = NSFileManager.defaultManager()
let fileContent = fileManager.contentsAtPath(soundFilePath)
objectsToShare.addObject(fileContent!)
if let objects = objectsToShare as [AnyObject]? {
let activityVC = UIActivityViewController(activityItems: objects, applicationActivities: nil)
if let subject = noteTitle.text as String? {
activityVC.setValue(subject, forKey: "subject")
}
self.presentViewController(activityVC, animated: true, completion: nil)
}
提前致谢:)
答案 0 :(得分:2)
您将文件内容放入objectsToShare
。 activityVC
可以共享内容,但不知道它是要共享的文件。
我认为你需要一个UIDocumentInteractionController
。像这个未经测试的代码:
var dic:UIDocumentInteractionController? // has to be instance variable not buried in a method, or the dic is deallocated before it can open the file.
func shareFile(filePath:String, fileName: String, from:UIBarButtonItem) {
let url = NSURL(fileURLWithPath: filePath)
dic = UIDocumentInteractionController(URL:url)
dic?.UTI = fileName
dic?.presentOptionsMenuFromBarButtonItem(from, animated: true)
}
如果在dic
方法中使shareFile
为局部变量,则在文件打开之前它往往会被释放,并且会出现Couldn't get file size for (null)
错误。把它变成类的实例变量。