简体   繁体   English

ReactNative iOS Spotify SDK

[英]ReactNative iOS Spotify SDK

I am following the Spotify SDK tutorial , and trying to make a RN module for my application. 我正在关注Spotify SDK教程 ,并尝试为我的应用程序创建一个RN模块。 This is my SpotifyModule.m code: 这是我的SpotifyModule.m代码:

#import "SpotifyModule.h"
#import "React/RCTLog.h"
#import "React/RCTBridge.h"


@implementation SpotifyModule


RCT_EXPORT_MODULE()


+ (id)sharedManager {
  static SpotifyModule *sharedManager = nil;
  @synchronized(self) {
    if (sharedManager == nil)
      sharedManager = [[self alloc] init];
  }
  return sharedManager;
}


RCT_EXPORT_METHOD(authenticate:(RCTResponseSenderBlock)callback)
{
  // Your implementation here
  RCTLogInfo(@"authenticate");
  self.auth = [SPTAuth defaultInstance];
  // The client ID you got from the developer site
  self.auth.clientID = @"8fff6cbb84d147e383060be62cec5dfa";
  // The redirect URL as you entered it at the developer site
  self.auth.redirectURL = [NSURL URLWithString:@"my-android-auth://callback"];
  // Setting the `sessionUserDefaultsKey` enables SPTAuth to automatically store the session object for future use.
  self.auth.sessionUserDefaultsKey = @"current session";
  // Set the scopes you need the user to authorize. `SPTAuthStreamingScope` is required for playing audio.
  self.auth.requestedScopes = @[SPTAuthPlaylistReadPrivateScope, SPTAuthUserReadPrivateScope];

  //save the login callback
  SpotifyModule *spotifyModule = (SpotifyModule *)[SpotifyModule sharedManager];
  spotifyModule.loginCallback = callback;

  //setup event dispatcher
  spotifyModule.eventDispatcher = [[RCTEventDispatcher alloc] init];
  [spotifyModule.eventDispatcher setValue:self.bridge forKey:@"bridge"];

  // Start authenticating when the app is finished launching
  dispatch_async(dispatch_get_main_queue(), ^{
    [self startAuthenticationFlow];
  });
}

- (void)startAuthenticationFlow
{
  // Check if we could use the access token we already have
  if ([self.auth.session isValid]) {
    // Use it to log in
      SpotifyModule *spotifyModule = (SpotifyModule *)[SpotifyModule sharedManager];
      NSString *accessToken = self.auth.session.accessToken;
      spotifyModule.loginCallback(@[accessToken]);
  } else {
    // Get the URL to the Spotify authorization portal
    NSURL *authURL = [self.auth spotifyWebAuthenticationURL];
    // Present in a SafariViewController
    self.authViewController = [[SFSafariViewController alloc] initWithURL:authURL];
    UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;

    [rootViewController presentViewController:self.authViewController animated:YES completion:nil];
  }
}

- (BOOL)  application:(UIApplication *)app
          openURL:(NSURL *)url
          options:(NSDictionary *)options
{
  // If the incoming url is what we expect we handle it
  if ([self.auth canHandleURL:url]) {
    // Close the authentication window
    [self.authViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    self.authViewController = nil;
    // Parse the incoming url to a session object
    [self.auth handleAuthCallbackWithTriggeredAuthURL:url callback:^(NSError *error, SPTSession *session) {
      if (session) {
        // Send auth token
        SpotifyModule *spotifyModule = (SpotifyModule *)[SpotifyModule sharedManager];
        NSString *accessToken = session.accessToken;
        spotifyModule.loginCallback(@[accessToken]);      }
    }];
    return YES;
  }
  return NO;
}

@end

The way I want to use it from the RN end, is call authenticate, with a callback for the access token. 我希望从RN端使用它的方式是调用身份验证,并使用访问令牌的回调。 I got this working on Android fine. 我在Android上工作得很好。

  Native.authenticate(function(token) {
    store.dispatch(actions.loginSuccess(token));
  });

On iOS, with the above code, I get to the attached screen, and when clicking Ok I get the following error: 在iOS上,使用上面的代码,我进入了附加的屏幕,当单击Ok时,我收到以下错误:

SpotiFind[5475:29641] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[SpotifyModule application:openURL:sourceApplication:annotation:]: unrecognized selector sent to class 0x10cb406f8' SpotiFind [5475:29641] ***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'+ [SpotifyModule应用程序:openURL:sourceApplication:annotation:]:无法识别的选择器发送到类0x10cb406f8'

So from my minimal ObjectiveC understanding, its trying to call a different method, than the one that the tutorial instructs to implement. 因此,从我最小的ObjectiveC理解,它试图调用一个不同的方法,而不是教程指示实现的方法。 Any recommendations on how to make this work ? 关于如何使这项工作的任何建议?

If its any relevant, I build against iOS 10, and use the latest Spotify iOS SDK 如果它有任何相关性,我会针对iOS 10构建,并使用最新的Spotify iOS SDK

ps I realize the name might be against some copyrighting, its just temp for development :) ps我意识到这个名字可能反对某些版权,它只是开发的温度:)

在此输入图像描述

Thanks to your tips (in the comments), we managed to make our Spotify authentication work with React-native. 感谢您的提示(在评论中),我们设法使Spotify身份验证与React-native一起使用。

We used the code from your Pastebin to create a reusable module so that nobody has to waste time anymore. 我们使用您的Pastebin中的代码创建一个可重用的模块,这样就没有人不再浪费时间了。

You can find the module here: emphaz/react-native-ios-spotify-sdk 你可以在这里找到模块: emphaz / react-native-ios-spotify-sdk

There is a tutorial for the setup and we even created a boilerplate project 有一个设置教程,我们甚至创建了一个样板项目

Thanks a lot Giannis ! 非常感谢Giannis!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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