web应用程序- Cordova iOS与Spotify iOS SDK -触发授权



我只是基于Cordova开发Web应用程序,但我有一个问题:我想在一个新的应用程序中包含Spotify。

Spotify有iOS SDK(测试版)和初学者教程。这工作得很好(在应用加载启动Auth)。

现在我想在我的WebApp中使用Cordova.exec();实现这一点(未加载-我想在按钮点击(由JavaScript触发)上验证。

我已经为此生成了一个Cordova插件-这是有效的。我可以通过Cordova.exec();触发一个方法。

This Method get trigger:

- (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;
}

从Debug输出中可以看到:我没有进入if()。但是我不知道为什么:loginURL看起来是正确的。

你在if语句中使用了错误的URL。此时,您需要验证在用户被跳出Safari进行身份验证后交给应用程序的URL,而不是使用SPAuth生成的URL。

你的项目还存在问题吗?也许我的Spotify iOS SDK插件可以提供帮助。我刚刚在插件注册表中发布了第一个版本。

您可以通过cordova命令行客户端安装插件:cordova plugin add com.timflapper.spotify .

如果你还没有添加ios平台:cordova platform add ios .

下面的代码是一个简单的例子,说明如何使用Spotify进行身份验证并播放单轨:

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);

最新更新