Cómo recargar widgets desde la hoja de intercambio, a través de una Extensión de Acción.
Mi aplicación tiene una extensión de acción que permite a los usuarios recopilar y guardar enlaces en todo el sistema. También tiene un widget para mostrar estos enlaces en la pantalla de inicio.
Cuando agrego enlaces desde la aplicación principal, llamo a WidgetCenter.shared.reloadAllTimelines()
para recargar todos los widgets, y todo funciona como se espera.
Pero cuando agrego enlaces desde la extensión de acción, los widgets no se recargan, incluso después de llamar a WidgetCenter.shared.reloadAllTimelines()
. Solo cuando voy a la aplicación principal es cuando los widgets se recargan.
¿Cómo puedo actualizar mis widgets para los cambios realizados a través de la extensión de acción desde la hoja de compartición?
davy.ai
Para actualizar los widgets con los cambios realizados mediante la Extensión de Acciones, debes usar la propiedad URL .widgetURL en tu extensión. Cuando un usuario guarda un enlace desde la extensión de acción, establece la propiedad widgetURL correspondiente con la URL del contenedor compartido donde se almacenan los datos. El widget se actualizará automáticamente cuando cambien los datos.
En la línea de tiempo del widget, en la implementación de
TimelineProvider
, deberás verificar si la propiedadwidgetURL
ha cambiado desde la última actualización, y si lo ha hecho, deberás obtener los nuevos datos y devolver una nuevaTimelineEntry
con la información actualizada.Aquí tienes un ejemplo de implementación:
En tu extensión de acción, establece la propiedad
widgetURL
con la URL del contenedor compartido al guardar un enlace de la siguiente manera:En la implementación de
TimelineProvider
, deberás verificar si la propiedadwidgetURL
ha cambiado desde la última actualización, y si lo ha hecho, deberás obtener los nuevos datos y devolver una nuevaTimelineEntry
con la información actualizada:“`swift
struct LinkWidgetEntry: TimelineEntry {
let date: Date
let link: Link
}
struct LinkWidgetProvider: TimelineProvider {
typealias Entry = LinkWidgetEntry
}
“`