Peter-Schorn / SpotifyAPI

A Swift library for the Spotify web API. Supports all endpoints.
https://peter-schorn.github.io/SpotifyAPI/documentation/spotifywebapi
MIT License
265 stars 32 forks source link

Issue with redirecting back to app when clicking agree in Safari window #50

Closed charlottemoche closed 11 months ago

charlottemoche commented 11 months ago

Hi!

I am very baby to swift and all of this so I could be completely missing something simple.

I am successfully able to open the Safari window within my app. I am also able to successfully login. However, when I click Agree, I am seeing "Safari cannot open the page because the address is invalid."

It seems I am receiving back a valid url when I print it, and yet it's having trouble with the redirect.

I am using SwiftUI and iOS 17.

In my developer dashboard, my redirect uri is personify://content. I am not certain this is actually correct. I had it working properly with a redirect using the SDK and personify:// but it won't work that way for this instance. So I added content.

At the top of my LoginView:

@Binding var loggedIn: Bool

LoginView also:

 Button(action: {
                    authorizeWithSpotify()
                }) 

and

func authorizeWithSpotify() {
        var cancellables: Set<AnyCancellable> = []
        let spotify = SpotifyAPI(
            authorizationManager: AuthorizationCodeFlowPKCEManager(
                clientId: "my-client-id"
            )
        )

        let codeVerifier = String.randomURLSafe(length: 128)
        let codeChallenge = String.makeCodeChallenge(codeVerifier: codeVerifier)
        let state = String.randomURLSafe(length: 128)

        let authorizationURL = spotify.authorizationManager.makeAuthorizationURL(
            redirectURI: URL(string: "personify://content")!,
            codeChallenge: codeChallenge,
            state: state,
            scopes: [
                .playlistModifyPrivate,
                .userModifyPlaybackState,
                .playlistReadCollaborative,
                .userReadPlaybackPosition
            ]
        )!

        let safariViewController = SFSafariViewController(url: authorizationURL)
        LoginView.keyWindow?.rootViewController?.present(
            safariViewController,
            animated: true
        )

    }

in my ContentView:

struct ContentView: View {
    @StateObject private var tabSelection = TabSelectionViewModel()
    @State private var isLoggedIn = false

    var body: some View {
        if isLoggedIn {
            TabView(selection: $tabSelection.selectedTab) {
                ProfileView()
                    .tag(0)
                    .tabItem {
                        Image(systemName: "person.fill")
                        Text("Profile")
                    }

                RecommendedView()
                    .tag(1)
                    .tabItem {
                        Image(systemName: "heart.fill")
                        Text("Recommended")
                    }

                SettingsView(loggedIn: $isLoggedIn)
                    .tag(2)
                    .tabItem {
                        Image(systemName: "gearshape.fill")
                        Text("Settings")
                    }
            }
            .environmentObject(tabSelection)
        } else {
            LoginView(loggedIn: $isLoggedIn)
        }
    }
}

i am just a little lost and would appreciate some assistance, thank you so much!

Peter-Schorn commented 11 months ago

Have you registered your custom URL scheme (personify) in your app? See the README of SpotifyAPIExampleApp for help. See also Apple's documentation.

charlottemoche commented 11 months ago

hmm yes i have registered my custom url and set the env vars in the run edit: ah okay now i'm no longer getting the safari error, going to check out the sample app. thank you!

Peter-Schorn commented 11 months ago

The apple documentation I linked to uses UIKit to handle the incoming URL. For SwiftUI, use the onOpenURL(perform:) modifier. See this article. Please confirm whether or not you are successfully able to redirect back to your app from the browser.

charlottemoche commented 11 months ago

confirmed, this was the ticket! thank u so much :)