1
votes

In a project I have to make, I am working with external accessories, and the framework ExternalAccessory.

(I can connect to one and only one accessory).

The connection is easy, sending data is not really difficult.

One thing I would like to do is to store in memory the last connected device, to try to reconnect to it automatically when the application relaunch. (after leaving, so after the first launch.)

So, my problem is that EAAccessory is a custom class object, and could not be stored into NSUserDefaults.

I saw this question to store custom objects :

How to store custom objects in NSUserDefaults

So I would create a NSData with it, but did not succeeded, because EAAccessory does not implements the encodeWithCoder and initWithCoder methods...

Any idea ? For this way or another way to process ?

2

2 Answers

0
votes

You should store logically/in a meaningful way that makes sense all the important and persisten properties of the EAAcessory instance in an NSDictionary and store this dictionary in NSUserDefaults. Then you will have to read back this dictionary and perform the inverse convertion (alloc-initting the EAAccessory instance, setting its properties, etc.)

0
votes

I think you can implement the missing methods into a custom EAAccessory category. Maybe somthing like this :

*** interface
@interface EAAccessory (CodecAdditions)
- (void)encodeWithCoder:(NSCoder *)aCoder;
- (id)initWithCoder:(NSCoder *)aDecoder;
@end

*** implementation 
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeInt:[self name] forKey:@"name"];
    [aCoder encodeInt:[self manufacturer] forKey:@"manufacturer"];
    ...
}

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self.name = [aDecoder decodeIntForKey:@"name"];
    self.manufacturer = [aDecoder decodeIntForKey:@"manufacturer";
    ...

    return self;
}