5

How do you persist a MPMediaItemCollection object using NSUserDefaults? The MPMediaItemCollection object is a single song selected from the iPod.

I have been struggling with this for hours! Anyone have any ideas or alternatives to NSUserDefaults or a conversion from MPMediaItemCollection, or anything?!

Thanks...

Adam Waite
  • 19,175
  • 22
  • 126
  • 148
  • possible duplicate of [Way to persist MPMediaItemCollection objects? (selected from iPod)](http://stackoverflow.com/questions/8247315/way-to-persist-mpmediaitemcollection-objects-selected-from-ipod) – Till Nov 23 '11 at 20:48
  • You can answer your own question and mark at it as correct. – Mark Adams Nov 24 '11 at 02:34
  • 1
    cool. That works well. You should take credit for it – JeffB6688 Jan 11 '12 at 16:45

1 Answers1

7

First convert/encode the MPMediaItemCollection to an NSData Object and store it using NSUserDefaults using:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:mediaItemCollection];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:data forKey:@"someKey"];
    [defaults synchronize];

From there, you can decode and use anywhere else in your app....

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:@"someKey"];
MPMediaItemCollection *mediaItemCollection = [NSKeyedUnarchiver unarchiveObjectWithData:data];
Adam Waite
  • 19,175
  • 22
  • 126
  • 148