Share custom data with today extension by NSUserDefaults

240 views Asked by At

this is my first post, so please be kind and constructive with me.

Im trying to share an array of custom objects with my Today Extension via NSuserDefauls.

I know that I can't put custom objects in NSUserdefaults so i coded them by NSKeyedArchiver - NSKeyedUnarchiver to transform them in NSData.

This is my custom class

    #import "Counter.h"

@implementation Counter

@dynamic city;
@dynamic address;
@dynamic event;
@dynamic date;
@dynamic time;
@dynamic counter;
@dynamic latitude;
@dynamic longitude;


- (void)encodeWithCoder:(NSCoder *)encoder
{

    //Encode properties, other class variables, etc
    [encoder encodeObject:self.city forKey:@"city"];
    [encoder encodeObject:self.address forKey:@"address"];
    [encoder encodeObject:self.event forKey:@"event"];
    [encoder encodeObject:self.date forKey:@"date"];
    [encoder encodeObject:self.time forKey:@"time"];
    [encoder encodeObject:self.counter forKey:@"counter"];
    [encoder encodeObject:self.latitude forKey:@"latitude"];
    [encoder encodeObject:self.longitude forKey:@"longitude"];


}
- (id)initWithCoder:(NSCoder *)decoder
{
    self = [super init];
    if( self != nil )
    {
        self.city = [decoder decodeObjectForKey:@"city"];
        self.address = [decoder decodeObjectForKey:@"address"];
        self.event = [decoder decodeObjectForKey:@"event"];
        self.date = [decoder decodeObjectForKey:@"date"];
        self.time = [decoder decodeObjectForKey:@"time"];
        self.counter = [decoder decodeObjectForKey:@"counter"];
        self.latitude = [decoder decodeObjectForKey:@"latitude"];
        self.longitude = [decoder decodeObjectForKey:@"longitude"] ;

    }
    return self;
}

@end

This is the coding function inside the app.

- (void)saveInUserDefault {

//Counter is my Custom class
//I'm Using MagicalRecord
NSArray *array = [Counter MR_findAllSortedBy:@"date" ascending:NO];

NSMutableArray *defaultMutableArray = [NSMutableArray arrayWithCapacity:array.count];

NSUserDefaults *defaultArray = [[NSUserDefaults alloc] initWithSuiteName:@"group.LorenzoLoRe.Everycount"];


for (Counter *cnt in array) {
    NSData *dataToDefault = [NSKeyedArchiver archivedDataWithRootObject:cnt];
    [defaultMutableArray addObject:dataToDefault];
}


[defaultArray setObject:defaultMutableArray forKey:@"counts"];
[defaultArray synchronize];
}

At this point looking at the log, data seems to be write in NSUserDefaults

And here i try to retrive the data in the Extension

-

 (void)takeLastCounts {

    NSUserDefaults *defaultArray = [[NSUserDefaults alloc] initWithSuiteName:@"group.company.product"];
    [defaultArray synchronize];

    NSArray  *dummyArray = [defaultArray arrayForKey:@"counts"];;
    NSMutableArray *arrayFromDefault= NULL;
    Counter *cter = nil;


   for (NSData *data in dummyArray) {
       cter = [NSKeyedUnarchiver unarchiveObjectWithData:data];
       [arrayFromDefault addObject:cter];
    }

    self.cnt_1 = arrayFromDefault[0];
    self.cnt_2 = arrayFromDefault[1];

    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterShortStyle];

    self.eventLabel.text = self.cnt_1.event;
    self.dateLabel.text = [formatter stringFromDate:self.cnt_1.date];
    self.counterLabel.text = self.cnt_1.counter.stringValue;
    self.timeLabel.text = self.cnt_1.time;

}

When i try to execute the Extension, theres no errors but just empty Arrays. In particular NSArray *dummyArray = [defaultArray arrayForKey:@"counts"]; seems to be empty, so somewhere the data don't flows.

What I'm doing wrong??

Any kind of tips will be ver appreciate.

0

There are 0 answers