Cordova iOS与Spotify iOS SDK-触发验证

Cordova iOS与Spotify iOS SDK-触发验证,ios,web-applications,cordova,sdk,spotify,Ios,Web Applications,Cordova,Sdk,Spotify,我正在开发基于Cordova的Web应用程序,但我有一个问题:我想在新应用程序中加入Spotify Spotify的iOS SDK(测试版)有一个初学者。工作正常(在应用程序加载时启动身份验证) 现在我想在我的WebApp中使用Cordova.exec()实现这一点(未加载-我希望在单击按钮时进行身份验证(由JavaScript触发) 我已经为它生成了一个Cordova插件,它可以工作。我可以通过Cordova.exec();触发一个方法 此方法将被触发: - (BOOL)startSpotif

我正在开发基于Cordova的Web应用程序,但我有一个问题:我想在新应用程序中加入Spotify

Spotify的iOS SDK(测试版)有一个初学者。工作正常(在应用程序加载时启动身份验证)

现在我想在我的WebApp中使用
Cordova.exec()实现这一点(未加载-我希望在单击按钮时进行身份验证(由JavaScript触发)

我已经为它生成了一个Cordova插件,它可以工作。我可以通过
Cordova.exec();
触发一个方法

此方法将被触发:

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

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

您在if语句中使用了错误的URL。此时,您需要验证用户跳出Safari进行身份验证后传递给应用程序的URL,而不是您使用
SPAuth
生成的URL。您的应用程序是否仍有问题项目?也许我的朋友能帮上忙。我刚刚将第一个版本发布到插件注册表

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

如果尚未添加ios平台,请添加:
cordova平台添加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);

该插件是如何添加到AngularJS项目的?目前,我在运行您答案中的代码时收到“错误:找不到变量:spotify”。我在您的github页面上安装了该插件。