Save and Load Data on Today Extensions (iOS 8)

10,644

Solution 1

You need to use app group identifier instead of com.* For instance:

NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:@"group.company.appgroup"];

Don't forget to synchronise when you store data

[shared synchronize];

Solution 2

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"];

Solution 3

You need to add the App Group stuff detailed under here and then if it actually worked (pretty iffy under beta) it should allow you to share NSUserDefault data like normal between the host and widget.

Edit: Normal NSUserDefaults does not work. Apple has implemented a new method. To use, simply redefine your NSUserDefaults instance like this:

NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:@"com.you.app.container"];

Solution 4

You first need the App Groups set up for both targets (application and the extension).

Then, use the

NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:@"group.company.myapp"];

to obtain the defaults object which you can read from/write to as usual.

If you want to be notified of changes to the defaults, use the NSUserDefaultsDidChangeNotification in your widget (or app).

For a step-by-step tutorial explaining all this, take a look at this blog post.

Share:
10,644
Massimo Piazza
Author by

Massimo Piazza

Updated on July 28, 2022

Comments

  • Massimo Piazza
    Massimo Piazza almost 2 years

    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 any data results lost. How could I solve this issue?

    This is my code:

    NSUserDefaults *defaults;
    
    - (void)viewDidLoad {
    
    [super viewDidLoad];
    
    defaults = [NSUserDefaults standardUserDefaults];
    NSArray *loadStrings = [defaults stringArrayForKey:@"savedStrings"];
    
    if ([loadStrings objectAtIndex:0] != nil) {
        [display setText:[NSString stringWithFormat:@"%@", [loadStrings objectAtIndex:0]]];
    }
    if ([loadStrings objectAtIndex:1] != nil) {
        calculatorMemory = [NSString stringWithFormat:@"%@", [loadStrings objectAtIndex:1]].doubleValue;
    }
    
    }
    
    
    - (IBAction)saveData:(id)sender {
    
    NSString *displayString;
    NSString *memoryString;
    
    NSArray *saveStrings = [[NSArray alloc] initWithObjects: displayString, memoryString, nil];
    
    
    defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:saveStrings forKey:@"savedStrings"];
    [defaults synchronize];
    
    
    }
    
  • Massimo Piazza
    Massimo Piazza about 10 years
    Thanks, however after reading more deeply the documentation, two days ago I solved the problem as you wrote!
  • Andrew
    Andrew about 10 years
    @BalestraPatrick Make sure app groups are configured correctly and the correct entitlements are configured for both your app target and your extension target, as well as in your App IDs and provisioning profiles.
  • Spentak
    Spentak almost 10 years
    followed tutorial and does not work with keyboard app extension
  • Spentak
    Spentak almost 10 years
    @MassimoPiazza I have followed steps and this does not work. I am working with keyboard app extension. Thoughts?
  • GondyB
    GondyB almost 10 years
    @Spentak did you enable App group in xcode project capabilities for both containing app and keyboard extension ?
  • Spentak
    Spentak almost 10 years
    @BenjG37 yes I did. Is it possible that user defaults are not working for keyboard extensions yet?
  • Lukas Petr
    Lukas Petr almost 10 years
    @Spentak well that's weird. What exactly isn't working? Also, the tutorial (as well as this SO question) is about Today extensions, the keyboard ones are much more limited - maybe they have some limit on NSUserDefaults or something.
  • Spentak
    Spentak almost 10 years
    Lukas - can you get user defaults working on a keyboard extension? I have tested it and cannot make a successful test case.
  • Spentak
    Spentak almost 10 years
    This does not appear to work on iOS Keyboard Extensions
  • CVertex
    CVertex almost 10 years
  • Muzammil
    Muzammil almost 10 years
    @Spentak : Have you get this working for keyboard extension? I am following same procedure but not working for me as well.
  • hagi
    hagi over 9 years
    Have you tried working with NSUserDefaultsDidChangeNotifications? I don't receive notifications and it seems other people don't either, but I couldn't find any official documentation so I'm still wondering if this is possible somehow.
  • edukulele
    edukulele almost 9 years
    hagi - did you found a solution how to make NSUserDefaultsDidChangeNotification work? For me it is not working as well. I can check that values are changing from the main app to the extension and I checked it with the timer who triggers every second. But notifications does not appear even if the values was changed. I use groups, I use initWithSuiteName for user defaults initiation. And I can reach values. So everything is working except notifying. And I add notification center observer to my extension via addObserver:selector:name:object: with object value nil. But it is not working.
  • Patel Jigar
    Patel Jigar over 8 years
    damm... it is not working for me i am trying from last 2 days in ios 9.1
  • Kara
    Kara about 8 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
  • minus_one
    minus_one about 8 years
    My answer include this library. How to use library must visit this link.I think very easy.Now I already delete that link.