【问题标题】:Sharing data between iOS app and iMessage extension在 iOS 应用和 iMessage 扩展之间共享数据
【发布时间】:2018-07-12 11:07:44
【问题描述】:

我想在我的 iOS 应用程序中使用一个文件,它是 iMessage 扩展名,这怎么可能?使用应用程序组/共享容器。请有人写几行。在我的应用程序中,我将数据保存到一个文件中,我想将其加载到我的 iMessage 扩展程序中。

let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
  url.appendingPathComponent(PersistanceFileName)

我将此代码用于我的 iOS 应用程序,但我知道 iMessage 扩展程序无法打开它。那么解决办法是什么?

【问题讨论】:

    标签: ios swift xcode imessage


    【解决方案1】:

    首先确保您已将应用组标识符添加到应用目标和 iMessage 扩展目标。您可以通过转到每个目标下的 Capabilities 选项卡并向下滚动到 App Group 部分来添加应用程序组标识符。将应用组标识符添加到两个目标后,您可以使用以下代码获取应用组的容器 url:

    let url = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.myApp.appGroupIdentifier")
    

    【讨论】:

    • 现在我看到扩展名中的文件,但它说:无法打开,因为您没有查看权限。我使用此代码保存数据: data = try PropertyListSerialization.data(fromPropertyList: propData, format: PropertyListSerialization.PropertyListFormat.binary, options: 0) try data.write(to: url)
    • 我已经更新了答案 - 确保您已将应用组标识符添加到应用的目标和 iMessage 目标。
    猜你喜欢
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多