Wednesday, August 1, 2018

Swift 4 Upload song to Apple Music Library

Leave a Comment

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?

2 Answers

Answers 1

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.

Answers 2

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 Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment