【问题标题】:File sharing using UIActivityViewController/ UIDocumentInteractionController not working - [can't share via AirDrop, Whatapp, iMessage, Mail, etc.]使用 UIActivityViewController/UIDocumentInteractionController 的文件共享不起作用 - [无法通过 AirDrop、Whatapp、iMessage、Mail 等共享]
【发布时间】:2021-04-09 19:14:23
【问题描述】:

在尝试使用设备的共享表功能共享我下载到我的应用程序的“.mp3”文件时,我能够从文档目录中获取该文件但无法共享它。

当我尝试通过 Whatsapp 分享时,我收到一条错误消息:

此项目无法共享。请选择其他项目。

如果我通过 AirDrop 共享,它只会显示“失败”(红色),而通过其他选项共享会导致一条空白消息(例如,一封没有附加文件的新电子邮件或一个没有任何内容的新消息)

以下是我目前尝试过的代码选项:

选项 1 - 使用 UIActivityViewController

let fileURL = NSURL(fileURLWithPath: FileURLFromDocumentsDirectory)

var filesToShare = [Any]()

filesToShare.append(fileURL)

let activityViewController = UIActivityViewController(activityItems: filesToShare, applicationActivities: nil)

self.present(activityViewController, animated: true, completion: nil)

选项 2 - 使用 UIDocumentInteractionController

let docController : UIDocumentInteractionController?
docController = UIDocumentInteractionController(url: URL(string: fileToSharePath)!)
docController?.delegate = self
docController?.presentOptionsMenu(from: self.view.frame, in:self.view, animated:true)

在这两个选项中,我确实获得了文件,但无法共享它。手机确实将共享识别为音频文件,但我无能为力。

示例网址:

file:///Users/UserName/Library/Developer/CoreSimulator/Devices/C8B09E62-091F-4E61-BA6C-3D9EC23LKC01/data/Containers/Data/Application/EF2CKKJF-AB35-55G5-B778-439812EFGGG5/Documents/音频文件.mp3

共享适用于文本,但不适用于音频文件。

我是否还需要启用某些功能或向 AppDelegate 添加一些代码?

感谢任何帮助。谢谢。

更新 #1:来自@dfd 建议的代码 [仍然不起作用]

let shareAction = UIContextualAction(style: .normal, title: "Share") { (action, view, completionHandler) in
            
    let fileURL = NSURL(fileURLWithPath: (downloadedFile?.documentDirectoryURL)!)

    let activityViewController = UIActivityViewController(activityItems: [fileURL], applicationActivities: nil)

    self.present(activityViewController, animated: true, completion: nil)
                
}

此外,对于所有这些选项,我在共享表中将音频文件 (mp3) 作为“录音”。

【问题讨论】:

  • 在哪一行将音频文件添加到 filesToShare?
  • 所以流程如下:文件下载并存储到磁盘 > 用户将这些文件加载​​到 tableView > 滑动以查看并单击共享操作 - 上面的代码在 'leadingSwipeActionsConfigurationForRowAt' 委托方法中调用,其中我首先调用该对象获取音频的 URL,然后在上面的代码中传递它。我错过了一步吗? (我确实在共享表中获得了音频文件,只是无法将其从那里传递给其他应用程序或 AirDrop)
  • 只是一个猜测,但您是否尝试过更具体的代码?要将 UIActivityViewController 与某些文件类型和/或活动类型一起使用,您可能需要 (a) 不将文件设为 [Any] 并 (b) 从排除的活动类型中删除它们。
  • 谢谢我再次尝试过,确保我没有将文件转换为任何文件,而是直接在 activityController 中传递文件 URL,但它仍然不起作用。我在 shareSheet 中获得的文件被识别为“录音”——它是 mp3 的正确类型吗?请参阅更新后的代码帖子以及您的建议(也许我做错了什么?)----而且我还没有为任何一个选项包含任何排除类型。

标签: ios swift uiactivityviewcontroller ios-sharesheet uidocumentinteractioncontroller


【解决方案1】:

您可以尝试在临时目录中创建此文件的副本并共享该副本,而不是共享原始文件。共享完成后,您可以将其删除或留给系统删除。

类似这样的东西(你可以添加更多关于文件和目录存在的检查):

let fileManager = FileManager.default
let tempDirectory = fileManager.temporaryDirectory
let tempPath = tempDirectory.path + "/" + fileToSharePath.lastPathComponent
let tempURL = URL(fileURLWithPath: tempPath)
try? fileManager.copyItem(atPath:fileToSharePath.path , toPath: tempPath)


let docController : UIDocumentInteractionController?
docController = UIDocumentInteractionController(url: URL(string: tempURL)!)
docController?.delegate = self
docController?.presentOptionsMenu(from: self.view.frame, in:self.view, animated:true)

【讨论】:

  • 谢谢。这是一种创造性的方法,但遗憾的是没有奏效。似乎面临同样的问题 - 无法通过任何渠道分享。我在共享时得到的 tempURL 是 file:///private/var/mobile/Containers/Data/Application/56C0AA16-BAE5-497B-AD21-57C6DFL5C137/tmp/audio.mp3 - 与原始方法一致但仍然不工作。另外,我更正了倒数第三行并直接传递了 tempURL,因为它已经被转换为 URL(不是字符串)——以防万一其他人尝试。
  • 我想知道我们是否需要在 plist 文件中添加一些条目或启用某些功能以启用共享...不过似乎无法弄清楚...
  • 有趣,不,我们不需要为此在 plist 中添加任何内容。您在共享时是否在控制台中看到任何错误?您是否也面临音频文件或其他文件的问题?
  • 完全没有错误。目前只有音频文件。我可以毫无问题地分享文字。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-27
  • 1970-01-01
  • 2014-06-15
相关资源
最近更新 更多