简体   繁体   中英

Cordova iOS with Spotify iOS SDK - Trigger Auth

I'm just developing Web Apps based on Cordova, but I have a problem: I want to include Spotify in a new App.

Spotify has the iOS SDK (beta) with a beginner Tutorial . That worked fine (On App load Start the Auth).

Now I would like to implement that in my WebApp using Cordova.exec(); (Not on load - I would like to Auth on Button Click (Triggered by JavaScript).

I've generated a Cordova Plugin for that - that worked. And i can trigger a Method via Cordova.exec(); .

This Method get triggered:

- (BOOL)startSpotifyAuth:(CDVInvokedUrlCommand*)command {
    // Create SPTAuth instance; create login URL and open it
    NSURL *loginURL = [[SPTAuth defaultInstance] loginURLForClientId:kClientId declaredRedirectURL:[NSURL URLWithString:kCallbackURL] scopes:@[@"login"]];

    // Opening a URL in Safari close to application launch may trigger an iOS bug, so we wait a bit before doing so.
    // [UIApplication performSelector:@selector(openURL:) withObject:loginURL afterDelay:0.1];

    NSLog(@"*** GOT THIS IN DEBUG CONSOLE ***");

    // Ask SPTAuth if the URL given is a Spotify authentication callback
    if ([[SPTAuth defaultInstance] canHandleURL:loginURL withDeclaredRedirectURL:[NSURL URLWithString:kCallbackURL]]) {

        NSLog(@"*** GOT THIS - NOT - IN DEBUG CONSOLE ***");

        // Call the token swap service to get a logged in session
        [[SPTAuth defaultInstance] handleAuthCallbackWithTriggeredAuthURL:loginURL tokenSwapServiceEndpointAtURL:[NSURL URLWithString:kTokenSwapURL] callback:^(NSError *error, SPTSession *session)
        {
             if (error != nil) {
                 NSLog(@"*** Auth error: %@", error);
                 return;
             }

             // Call the -playUsingSession: method to play a track
             [self playUsingSession:session];
        }];
        return YES;
    }
    return NO;
}

As you can see by the Debug Outputs: I did not get inside the if(). But I don't know why: The loginURL looks correct.

You're using the wrong URL in your if statement. At that point, you need to validate the URL that gets handed to your application after the user has been bounced out to Safari for authentication, NOT the one you generate using SPAuth .

Are you still having issues with your project? Maybe my Spotify iOS SDK plugin can help. I just published the first version to the plugin registry.

You can install the plugin via the cordova command line client: cordova plugin add com.timflapper.spotify .

Add the ios platform if you haven't already done so: cordova platform add ios .

The following code is a simple example of how to authenticate with Spotify and play a single track:

var session, player;

var urlScheme = 'your-custom-url-scheme';
var clientId = 'your-own-client-id';

function onDeviceReady() {
  spotify.authenticate(urlScheme, clientId, 'token', authDone);
}

function authDone(error, sess) {
  if (error) return console.log("ERROR!", error);

  console.log(sess);

  session = sess;

  player = spotify.createAudioPlayer(clientId);

  player.login(session, function(error) {
    if (error) return console.log(error);

    player.play('spotify:track:2DlfLPbXH5ncf56Nytxd4w', function(error) {
      if (error) return console.log(error);
    });
  });
}

document.addEventListener('deviceready', onDeviceReady, false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM