9

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?

matt
  • 515,959
  • 87
  • 875
  • 1,141
user979331
  • 11,039
  • 73
  • 223
  • 418
  • 1
    Adding a bounty isn't going to help. There is _no way_ to do what you are asking to do. You _cannot_ modify the user's music library. The end. Full stop. Accept that and move on. – matt Jul 30 '18 at 14:34
  • What about this way? https://stackoverflow.com/a/17433006/979331 If I go this route and submit my app will it be rejected? – user979331 Jul 30 '18 at 14:39
  • That's a MacOS answer. It is irrelevant. You are programming iOS. You will not "go this route". You can't. I repeat, you cannot in any way modify the user's music library. – matt Jul 30 '18 at 14:40
  • Okay, so I guess I am going to the route of saving the song locally in my application DocumentDirectory? But can you confirm one thing for me...by saving the song locally in the application Document Directory, I will be able to listen to the song offline? Also, how does Shazam add songs to Apple Music Library? – user979331 Jul 30 '18 at 14:47
  • "But can you confirm one thing for me...by saving the song locally in the application Document Directory, I will be able to listen to the song offline? Also, how does Shazam add songs to Apple Music Library?" If you have a new question, please ask a new question. Discussion here is about the question you _did_ ask. (I do not know Shazam, so I don't know whether you are making a false assumption. But I assure you that modification of the user's music library is not in your power. It is in the _user's_ power.) – matt Jul 30 '18 at 14:48
  • https://stackoverflow.com/a/17433006/979331 is for MacOS. The Apis are different for iOS for MacOS, though they may use the same programming language and common design patterns. – Md. Ibrahim Hassan Jul 31 '18 at 08:52
  • @user979331 Did you find any solution to save audio file in music library? – Ekta Padaliya Oct 07 '19 at 07:04

2 Answers2

10

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.

Md. Ibrahim Hassan
  • 5,359
  • 1
  • 25
  • 45
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/