Thursday, July 4, 2024
HomeiOS Developmentios - UserDefaults returns null, however ought to return knowledge

ios – UserDefaults returns null, however ought to return knowledge


import WidgetKit
import SwiftUI

personal let widgetGroupdId = "group.home_widget"

extension View {
    func widgetBackground(_ backgroundView: some View) -> some View {
        if #out there(iOSApplicationExtension 17.0, *) {
            return containerBackground(for: .widget) {
                backgroundView
            }
        } else {
            return background(backgroundView)
        }
    }
}


struct Supplier: TimelineProvider {
    func placeholder(in context: Context) -> SimpleEntry {
        SimpleEntry(date: Date(), metropolis: "PlaceHolder", temperature: "2°", weatherDescription: "Bulutlu", feelsLikeTemperature: "0°")
    }
    
    func getSnapshot(in context: Context, completion: @escaping (SimpleEntry) -> ()) {
        let entry: SimpleEntry
        if context.isPreview{
          entry = placeholder(in: context)
        }
        else{
           
          let userDefaults = UserDefaults(suiteName: widgetGroupdId)
          let metropolis = userDefaults?.string(forKey: "metropolis") ?? "Tirana"
            print("Metropolis from UserDefaults: (metropolis)")              
            if let metropolis = userDefaults?.string(forKey: "metropolis") {
                print("Metropolis from UserDefaults in predominant app: (metropolis)")
            } else {
                print("Metropolis not present in UserDefaults")
            }

            
          entry = SimpleEntry(date: Date(), metropolis: metropolis, temperature: "2°", weatherDescription: "Bulutlu", feelsLikeTemperature: "0°")
        }
          completion(entry)
      }

    
    func getTimeline(in context: Context, completion: @escaping (Timeline<Entry>) -> ()) {
        getSnapshot(in: context) { (entry) in
            let timeline = Timeline(entries: [entry], coverage: .atEnd)
            completion (timeline)
        }
    }
    
    
    
    struct SimpleEntry: TimelineEntry {
        let date: Date
        let metropolis: String
        let temperature: String
        let weatherDescription: String
        let feelsLikeTemperature: String
    }
    
  
        


    struct home_widget_Previews: PreviewProvider {
        static var previews: some View {
           home_widgetEntryView(entry: SimpleEntry(date: Date(),metropolis: "Instance metropolis", temperature: "2°", weatherDescription: "Buludlu", feelsLikeTemperature: "0°"))
                .previewContext(WidgetPreviewContext(household: .systemSmall))
            
        }
    }
}
@predominant
    struct home_widget: Widget {
        let sort: String = "home_widget"
        var physique: some WidgetConfiguration {
            StaticConfiguration(sort: sort, supplier: Supplier()) { entry in
                home_widgetEntryView(entry: entry)
            }
            .configurationDisplayName ("My Widget")
            .description("That is an instance widget.")
            .contentMarginsDisabled()
            
        }
    }
    
struct home_widgetEntryView : View {
    var entry: Supplier.Entry
  
    var physique: some View {
        VStack(alignment: .main) {
            HStack {
                Picture("navigation")
                    .foregroundColor(Shade.white)
                Spacer().body(width: 5)
                Textual content(entry.metropolis).foregroundColor(.white).font(Font.system(dimension: 18))
                       }
            
        }
        .widgetBackground( Picture("cloud")
            .resizable()
            .aspectRatio(contentMode: .fill )
            
            
        )
        .contentMargins(0)
        .padding(0)
        
      
    }
}

Right here in UserDefaults I do not get metropolis, nonetheless in my Flutter app I replace this information. I initialize residence widget at predominant dart, after which

HomeWidget.saveWidgetData<String>(
      'metropolis',
      metropolis,
    );

replace my metropolis.

In android every thing works good. I get knowledge. However in iOS there’s some drawback. I believe the issue is with swift code. Thanks upfront for assist.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments