Persist a MPMediaItemCollection Object Using NSUserDefaults

前端 未结 1 372
清酒与你
清酒与你 2021-01-01 02:42

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

I have been struggl

相关标签:
1条回答
  • 2021-01-01 03:25

    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];
    
    0 讨论(0)
提交回复
热议问题