Note that there are some explanatory texts on larger screens.

plurals
  1. POWay to persist MPMediaItemCollection objects? (selected from iPod)
    primarykey
    data
    text
    <p>I am making an app in which the user can select a song in a settings tab and have this played in a different view on demand. I want it so that this item can be stored if the user is to shut the app and reopen it another time.</p> <p>I have managed to allow the user to select and store a song in with: </p> <pre><code>-(IBAction)showMediaPicker:(id)sender{ MPMediaPickerController *mediaPicker = [[MPMediaPickerController alloc] initWithMediaTypes: MPMediaTypeAny]; mediaPicker.delegate = self; mediaPicker.allowsPickingMultipleItems = NO; mediaPicker.prompt = @"Select Alarm Sound"; [self presentModalViewController:mediaPicker animated:YES]; } - (void) mediaPicker: (MPMediaPickerController *) mediaPicker didPickMediaItems: (MPMediaItemCollection *) mediaItemCollection { [self dismissModalViewControllerAnimated: YES]; settingsData.selectedSong = mediaItemCollection;//Object of type MPMediaItemCollection </code></pre> <p>but I want the user to have to do this every time they use the app.</p> <p>I have tried using NSUserDefaults: </p> <pre><code>NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:settingsData.selectedSong forKey:@"alarmSoundKey"]; [defaults synchronize]; </code></pre> <p>but get the error:</p> <p><em>*</em> -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '' of class 'MPMediaItemCollection'. Note that dictionaries and arrays in property lists must also contain only property values.</p> <p>What are my options please? Not really sure how to tackle this one...</p> <p>SOLUTION - </p> <p>I can't answer my own questions yet so I'll put it up here:</p> <p>I HAVE FOUND MY OWN SOLUTION TO THIS:</p> <p>First convert/encode the MPMediaItemCollection to an NSData Object and slam store it using NSUserDefaults using:</p> <pre><code>NSData *data = [NSKeyedArchiver archivedDataWithRootObject:mediaItemCollection]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:data forKey:@"someKey"]; [defaults synchronize]; </code></pre> <p>From there, you can decode and use anywhere else in your app....</p> <pre><code>NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSData *data = [defaults objectForKey:@"someKey"]; MPMediaItemCollection *mediaItemCollection = [NSKeyedUnarchiver unarchiveObjectWithData:data] </code></pre> <p>Hope that is some help to someone. Spread the word, this hasn't been covered enough. Have literally been working on this problem for about 4 hours...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload