gpt4 book ai didi

ios - 通过 sharesheet 共享音频文件

转载 作者:可可西里 更新时间:2023-11-01 01:37:27 26 4
gpt4 key购买 nike

我正在尝试使用 iOS 的共享表功能共享音频文件。我无法将音频文件音频文件放入共享表中进行共享。目前,电子邮件中没有添加附件或音频文件(例如)。

我有以下检索音频文件的代码。我可以使用 MFMailComposeViewController 将其作为电子邮件明确发送,但我更愿意将其“扔”到共享表中,以允许用户将文件上传到电子邮件/保管箱/谷歌驱动器。

这可能吗?

    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)
}

提前致谢:)

最佳答案

您将文件内容 放入objectsToShareactivityVC 可以共享内容,但不知道它是要共享的文件。

我想你需要一个 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)
}

如果您在 shareFile 方法中将 dic 设置为局部变量,它往往会在文件打开之前被释放,这会让您成为 Couldn' t get file size for (null) 错误。改为将其作为类的实例变量。

关于ios - 通过 sharesheet 共享音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35155725/

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