如何在WidgetKit中重置意图扩展配置 [英] How to reset intent extension configurations in WidgetKit

查看:98
本文介绍了如何在WidgetKit中重置意图扩展配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户可以登录的应用程序.如果用户已登录,则我将为我的窗口小部件显示一个占位符,并且可以通过意图扩展对其进行配置.

I have an application where a user can login. If a user is logged in, then I display a placeholder for my widget and it's configurable through an intent extension.

配置有两个选项:

  • 第一个选项取决于当前登录用户的用户名.
  • 第二个选项取决于第一个选项的值.

这对于1个用户来说效果很好,但是,如果该用户注销,然后使用其他帐户登录,则当他们尝试配置窗口小部件时仍会选择旧的选定选项,这是错误的选项,因为现在已经不一样了用户.此外,旧数据仍显示在小部件中.

This works fine for 1 user, however, if the user logs out, then logs in with a different account, the old selected options are still selected when they try to configure the widget, which are wrong options since it's now a different user. Also, the old data is still shown in the widget.

如何在WidgetKit中重置窗口小部件的配置?这意味着,当用户尝试对其进行配置时,所有先前的配置都将被取消选择并且为空.

How do I reset the configuration of a widget in WidgetKit? Meaning, when the user tries to configure it all the previous configurations would be unselected and empty.

我尝试过

WidgetCenter.shared.reloadAllTimelines()

但这只会重新加载时间轴方法,而不会重置配置.

But that only reloads the timeline methods but does not reset configurations.

我查看了Apple的文档,也找不到任何相关内容.

I've looked at Apple's documentations and could not find anything about it either.

非常感谢

推荐答案

我找到了此解决方案,但仅针对动态属性:为IntentHandler中的每个用户使用不同的标识符. 示例:

I found this solution, but for dynamic properties only: use different identifiers for every user in your IntentHandler. Example:

class IntentHandler: XXExtension { ... }

extension IntentHandler: XXDynamicXXXSelectionIntentHandling {

   func provideXXXOptionsCollection(for intent: XXDynamicXXXSelectionIntent, with completion: @escaping (XXObjectCollection<XXX>?, Error?) -> Void) {
      let userId = (UserDefaults.myGroup().object(forKey: "UserId" ) as? String) ?? ""
      let items: [XXX] = UserDefaults.myGroup().sharedXxx.map { (sharedXxx) -> XXX in
         return XXX(identifier: userId + "_" + sharedXxx.Id // <== add unique prefix ===
                       display: sharedXxx.visibleName())
      }        
      let collection = XXObjectCollection(items: items)
      completion(collection, nil)
   }

}

这篇关于如何在WidgetKit中重置意图扩展配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆