微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

与AppGroup共享数据

如何解决与AppGroup共享数据

我想将UIKit文件中的一个变量共享给使用SwiftUI创建的Widget扩展。 I followed this here.请查看J arango的回答。

但是我不明白那里的最后一部分。 我必须使用import MySharedobjects

所以我这样做了:

    import MySharedobject

struct testing {
    let mySharedobject = MySharedobject(name: "My Name",lastName: "My Last Name")
                       
     do {
         let data = try JSONEncoder().encode(mySharedobject)
    
          /// Make sure to use your "App Group" container suite name when saving and retrieving the object from UserDefaults
          let container = UserDefaults(suiteName:"group.com.widgetTest.widgetContainer")
              container?.setValue(data,forKey: "sharedobject")
                            
          /// Used to let the widget extension to reload the timeline
          WidgetCenter.shared.reloadAllTimelines()
    
          } catch {
            print("Unable to encode WidgetDay: \(error.localizedDescription)")
       }
}

但是我遇到以下错误

  • 调用中#1,#2的位置有额外的参数
  • 呼叫中缺少参数的参数
  • 插入来自:
  • 使用do部分的预期声明。

解决方法

  1. 在主应用程序中将数据保存到 public bool _isServerAvailableLabelVisible = false; public bool IsServerAvailableLabelVisible { get { return _isServerAvailableLabelVisible; } set { SetProperty(ref _isServerAvailableLabelVisible,value); } }
UserDefaults
  1. 从小部件中的UserDefaults(suiteName: <your_app_group>)!.set("test",forKey: "test") 读取数据:
UserDefaults

如果要保存其他类型,请参见:

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。