iosswiftauthenticationstorekitapple-musickit

iOS 14 StoreKit - SKCloudServiceController's requestusertoken not being called properly


I am trying to play songs on iOS 14 using Apple Music API. I have the developer's token, and I have asked for the permission for accessing the user's apple music.

However, when I call requestusertoken api, its closure never gets called, so obviously I don't receive anything from the request - not even an error. It's driving me crazy.

Here is my code. What am I doing wrong?

func getUserToken() -> String {
var userToken = String()

let lock = DispatchSemaphore(value: 0)

SKCloudServiceController().requestUserToken(forDeveloperToken: developerToken) { (receivedToken, error) in
    guard error == nil else { return }
    if let token = receivedToken {
        userToken = token
        lock.signal()
    }
}

lock.wait()
return userToken }

Solution

  • I've tried the code and there were two major problems.

    First, DispatchSemaphore makes the return line execute too early. Second, original developer token doesn't work due to latest iOS 14.3 issue.

    So, I first erased DispatchSemaphore.

    func getUserToken() {
        var userToken = String()
        
        SKCloudServiceController().requestUserToken(forDeveloperToken: developerToken) { (receivedToken, error) in
            guard error == nil else { return }
            
            if let token = receivedToken {
                userToken = token
                print(userToken)
            }
        }
    }
    

    Then tweaked developer token following this repository.

    Now, it's printing user token properly. I hope this helped.