Tuesday, July 30, 2024
HomeiOS Developmentios - UserDefaults: Easy methods to learn and write information for WidgetKit

ios – UserDefaults: Easy methods to learn and write information for WidgetKit


I’m revisiting one my apps which I take advantage of WidgetKit, it labored positive in it is implementation nevertheless when working for iOS 17 with Xcode 15 the information now not reads/writes from consumer defaults.

From the principle iOS app I write to the UserDefaults

// Set
let checklist = ["one", "two", "three"]
var AppGroup: String {
    "group.com.appname.com.contents"
}

let defaults = UserDefaults(suiteName: AppGroup)!

defaults.set(funds, forKey: "todolist")

Then I learn the information on the Widget extension like so

class DataHelper {

    var checklist: [String] {
        return defaults!.object(forKey: "todolist") as? [String] ?? [String]()
    }

}

Within the WidgetView

struct ListWidget: Widget {
    let sort: String = "ListWidget"
    
    let dataHelper = DataHelper()

    var physique: some WidgetConfiguration {
        StaticConfiguration(sort: sort, supplier: ListProvider()) { entry in
            ListWidgetView(budgets: dataHelper.todolist)
        }
        .configurationDisplayName("Listing")
        .description("See your Listing")
        .supportedFamilies([.systemSmall])
    }
}


struct ListWidgetView : View {
    
    @Atmosphere(.widgetFamily) var household: WidgetFamily
    
    var checklist: [String]
    
    @ViewBuilder
    var physique: some View {
        change household {
        case .systemLarge:  ListOverview(checklist: todolist)
        default:  ListOverview(budgets: checklist)
        }
    }
}

struct ListOverview : View {
    
    var checklist: [String]

    var todolist: [String[ {
        if list.isEmpty {
            return "no list". <-- THIS IS RETURNED
        } else {
            return list[0]
        }

    }
    
    var physique: some View {
        // Progress Ring
        ZStack(alignment:.heart) {
            //Label
            VStack(alignment: .heart, spacing: 8) {
                Textual content(todolist)
                    .foregroundStyle(.white)
                    .lineLimit(1)
            }
        }
        .containerBackground(Shade.black, for: .widget)
    }
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments