Save and Load Data on Today Extensions (iOS 8)

后端 未结 5 1168
陌清茗
陌清茗 2020-12-19 05:10

Is it possible to save and load data on Today Extension using NSUserDefaults? After closing the Notification Center, the widget behaves like an app which is terminated, so a

5条回答
  •  死守一世寂寞
    2020-12-19 05:25

    For anyone wondering how in the world do you save and get values then look at this code.

    In your regular app add this to save whatever you like in your *.m file.

    NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:@"group.yourcompanyname.TodayExtensionSharingDefaults"];
    
        //save dic
        [shared setObject:dictionary2 forKey:@"dicForTodayWidget"];
    
        //save array
        [shared setObject:tempArray2 forKey:@"arrayForTodayWidget"];
    
        //save some value
        [shared setObject:@"1234" forKey:@"myValForTodayWidget"];
    
        [shared synchronize];
    

    In your today widget under TodayViewController.m in viewDidLoad add this.

    NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:@"group.yourcompanyname.TodayExtensionSharingDefaults"];
    
        //get dic
        NSMutableDictionary *dictionary = [shared objectForKey:@"dicForTodayWidget"];
    

提交回复
热议问题