Persist a MPMediaItemCollection Object Using NSUserDefaults

自闭症网瘾萝莉.ら 提交于 2019-12-18 13:37:12

问题


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


回答1:


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];


来源:https://stackoverflow.com/questions/8248821/persist-a-mpmediaitemcollection-object-using-nsuserdefaults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!