【问题标题】:Cordova iOS with Spotify iOS SDK - Trigger Auth带有 Spotify iOS SDK 的 Cordova iOS - 触发身份验证
【发布时间】:2014-04-30 06:55:51
【问题描述】:

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

Spotify 拥有 iOS SDK(测试版),初学者 Tutorial。效果很好(在应用程序加载时启动身份验证)。

现在我想在我的 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 看起来是正确的。

【问题讨论】:

    标签: ios web-applications cordova sdk spotify


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您的项目还有问题吗?也许我的Spotify iOS SDK plugin 可以提供帮助。我刚刚将第一个版本发布到插件注册表。

      您可以通过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);
      

      【讨论】:

      • 如何将插件添加到 AngularJS 项目中?目前,在您的答案中运行代码时,我收到“错误:找不到变量:spotify”。我根据你的 github 页面安装了插件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      • 2017-10-30
      • 1970-01-01
      • 1970-01-01
      • 2016-04-28
      • 2015-04-15
      • 2017-05-10
      相关资源
      最近更新 更多