・(通常は親側のビューコントローラで) MPMediaPickerControllerDelegateのメソッドを2つ実装する
・-[MPMediaPickerController presentViewController:animated:completion]を実行すると音楽を選択するビューに切り替わる- (void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection {NSURL *url = [mediaItemCollection.items.firstObject valueForProperty:MPMediaItemPropertyAssetURL];if (url)[self _openFileWithURL:url];[mediaPicker dismissViewControllerAnimated:YES completion:nil];}- (void)mediaPickerDidCancel:(MPMediaPickerController *)mediaPicker {[mediaPicker dismissViewControllerAnimated:YES completion:nil];}
・シミュレータではiTunesMusicLibraryを利用できないので実機で動作確認を行う。MPMediaPickerController *picker = [[MPMediaPickerController alloc] initWithMediaTypes:MPMediaTypeMusic];picker.delegate = self;picker.showsCloudItems = NO;picker.showsItemsWithProtectedAssets = NO;[self presentViewController:picker animated:YES completion:nil];
・MPMediaPickerControllerではDRM保護された音楽やiCloud上の音楽のURLを参照できないようです。
参考:
ios - MPMediaPickerController shows an empty screen on iOS10 - Stack Overflow
iphone - Can i access iPod Library on simulator? - Stack Overflow