Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Persist a MPMediaItemCollection Object Using NSUserDefaults

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...

like image 995
Adam Waite Avatar asked Nov 23 '11 20:11

Adam Waite


1 Answers

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];
like image 143
Adam Waite Avatar answered Nov 17 '22 07:11

Adam Waite