iOS Spotify SDK Problem when using StoryBoard (Scenes) in Xcode. Swift demo scenario works though.

Solved!
Reply
Highlighted

iOS Spotify SDK Problem when using StoryBoard (Scenes) in Xcode. Swift demo scenario works though.

Casual Listener

My info

Plan: Premium

Country: Belgium

Device: iPhone 11 Pro Max

Operating System: iOS 13.5 + macOS Catalina

 

Dear all

 

I installed and the iOS SDK properly in Xcode and set up the server token swap and refresh properly => demo swift login works nicely.

 

Now I'm trying to integrate that into my iOS application and it doesn't work at all. I tested everything that was possible and I broke it down to the least changes to the demo to better understand what could cause it to fail. See hereunder:

 

1) I set up a basic Xcode iOS app

2) I don't use create the views using programmation as in your demo, I use the Storyboard (only difference with the demo code)

3) I set everything exactly the same, it builds and it runs on the iPhone device nicely without any error or any alert message

4) when I click "Connect", it asks permission to use "Spotify" as expected, I answer yes, it switches to Spotify and starts playing the song

5) it switches back to my app and still shows the "Connect" button

 

When debugging, I set up breakpoints at every sessionManager, application methods and appRemote event functions, none of them is apparently caught.

When I do the same in the demo project, it stops at breakpoints

 

My conclusions:

- the delegates do not work and seem not to be assigned correctly because no event is caught

 

What is strange? It's line per line EXACTLY the same code as the demo project, only I use Scenes so I have a SceneDelegate.swift file in my project.

 

I tried to transfer some of the code like the delegate assignment to the SceneDelegate file to the corresponding events but it doesn't work well and the openURL method is not the same as in the application open url method so I don't know how to translate the openURL call at scene level.

 

I'm lost, I spent 3 days trying everything and nothing works. BTW, I use regularly the Web API without any problem so I understand pretty well the Spotify auth process.

 

Can anybody help me? Because the iOS examples, to be honest, they are pretty thin to find solutions. Can't you at least have one example that uses the StoryBoard or SwiftUI, building the views programmatically do not really help people that want to start programming.

 

Or at least give me tips please, I'm stuck..

 

thx a lot, Nico

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Solution!

Re: iOS Spotify SDK Problem when using StoryBoard (Scenes) in Xcode. Swift demo scenario works thoug

Regular
You are right. This issue is caused by SceneDelegate. Two ways to solve this problem.
1. Add openURLContexts func in SceneDelegate file to process the same thing of openURL in appdelegate.
- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts {
UIOpenURLContext *ctx = [URLContexts allObjects][0];
ViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootVC.sessionManager application:[UIApplication sharedApplication] openURL:ctx.URL options:ctx.options];
}
2. remove UISceneConfigurations section from info.plist. Then it will callback to openurl.

View solution in original post

5 Replies
Highlighted

Re: iOS Spotify SDK Problem when using StoryBoard (Scenes) in Xcode. Swift demo scenario works thoug

Regular

You forget one thing in AppDelegate.m. Add the code to process the response from spotify. After this process , delegate of sessionManager will be called.

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {

        rootViewController.sessionManager.application(app, open: url, options: options)

        return true

}

Highlighted

Re: iOS Spotify SDK Problem when using StoryBoard (Scenes) in Xcode. Swift demo scenario works thoug

Casual Listener

Well, thanks for the reply but actually it's a storyboard scene so that method is never called, only the equivalent ini SceneDelegate.swift is called.

Highlighted
Solution!

Re: iOS Spotify SDK Problem when using StoryBoard (Scenes) in Xcode. Swift demo scenario works thoug

Regular
You are right. This issue is caused by SceneDelegate. Two ways to solve this problem.
1. Add openURLContexts func in SceneDelegate file to process the same thing of openURL in appdelegate.
- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts {
UIOpenURLContext *ctx = [URLContexts allObjects][0];
ViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootVC.sessionManager application:[UIApplication sharedApplication] openURL:ctx.URL options:ctx.options];
}
2. remove UISceneConfigurations section from info.plist. Then it will callback to openurl.

View solution in original post

Highlighted

Re: iOS Spotify SDK Problem when using StoryBoard (Scenes) in Xcode. Swift demo scenario works thoug

Casual Listener

Hi Ouml

 

Fantastic, it works fine now.

 

Just another problem now, it seems that my IBOutlets are not accessible when the interface has to be refreshed with the playing song.

 

I instantiate the Viewcontroller in SceneDelegate and keep a reference as follows:

 

let rootViewController : ViewController = {

        return (UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(identifier: "ViewController") as? ViewController)!

    }()

 

I have outlets as such:

 

@IBOutlet weak var imgCover: UIImageView!

    @IBOutlet weak var btnPausePlay: UIButton!

    @IBOutlet weak var lblTrack: UILabel!

    @IBOutlet weak var btnConnect: UIButton!

    @IBOutlet weak var btnDisconnect: UIButton!

 

The outlets are connected and they are accessible when I am in viewDidLoad function of the view controller.

 

But, as soon as I want to change them in other functions, it returns "

Fatal error: Unexpectedly found nil while unwrapping an Optional value"

 

As if the outlets are not initialised in the class (although using self. )

What is strange is that the initialise above is called TWICE when I debug, I have the feeling that I have two instances of the same view controller active at some points

 

Well, that's another issue, thanks for your precious help

 

 

Highlighted

Re: iOS Spotify SDK Problem when using StoryBoard (Scenes) in Xcode. Swift demo scenario works thoug

Regular

Add a log in File ViewController->ViewDidLoad() to see whether the ViewController has been init twice.

 

If Yes, Check [General]->[Deployment Info]->"Main Interface". Has the value of this option been removed?

 

If not this problem, which func do you put your init code in? Try to find why it has been init twice. 

SUGGESTED POSTS