I am building an iOS app in Swift 4 and I am trying to add a song to the users Apple Music Library, I am able to get my Apple Music Library like so:
mediaPicker = MPMediaPickerController(mediaTypes: .anyAudio)
mediaPicker?.showsCloudItems = false
mediaPicker?.showsItemsWithProtectedAssets = false
if let picker = mediaPicker{
picker.delegate = self
picker.allowsPickingMultipleItems = false
present(picker, animated: true, completion: nil)
}
But is it possible to upload a song to the users Apple Music Library?
Here is how I am getting the audio file that I wish to add to the users Apple Music Library
let audioString = (result[0]["audio"] as! String).addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
let audioUrl = URL(string:"http://example.com/uploads/" + audioString!)
Alamofire.request(audioUrl!).responseData { response in
do {
self.audio = try AVAudioPlayer(data: response.data!)
self.audio?.prepareToPlay()
}catch{
}
}
UPDATE
I found this: https://stackoverflow.com/a/17433006/979331 Now I have 2 questions, how would I convert this to Swift and if I use ScriptingBridge will this be allowed when I submit my app to the app store?
In the Music app on your Mac, choose File > Add To Library or File > Import. If you selected “Copy files to Music Media folder when adding to library” in Files settings, you see File > Import. Locate a file or folder, then click Open. If you add a folder, all the files it contains are added to your library.
As we mentioned before, it's not possible to directly upload your music to Apple Music so you will need to go through a music distributor to get your music on streaming platforms.
According to the Official Documentation
An MPMediaPickerController object, or media item picker, is a specialized view controller that you employ to provide a graphical interface for selecting media items.
This makes it clear that MPMediaPickerController cannot be used for uploading songs into the music library. It is intended to be used for fetching items from the User Media Library.
So even if you download the songs to your application. You cannot transfer it to the Music Library.
Hope this helps.
The Apple have not provided any provision to upload the song directly to Apple Music library. Rather you can save your song locally in your application DocumentDirectory for later use.
For more Information, you can refer to this below blog:
https://mobikul.com/play-audio-file-save-document-directory-ios-swift/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With