2017-01-16 295 views
3

我正在关注Spotify SDK tutorial,并试图为我的应用程序创建一个RN模块。这是我的SpotifyModule.m代码:ReactNative iOS Spotify SDK

#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 

我想从RN端使用它的方式,就是调用身份验证,与访问令牌的回调。我在Android上工作得很好。

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

在iOS上,与上面的代码,我得到本地的屏幕上,当单击确定,我得到以下错误:

SpotiFind[5475:29641] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[SpotifyModule application:openURL:sourceApplication:annotation:]: unrecognized selector sent to class 0x10cb406f8'

从我最小的ObjectiveC理解

因此,它试图调用一个不同于本教程指导实施的方法。 关于如何使这项工作的任何建议?

如果它的任何有关,我对打造的iOS 10,并使用最新的Spotify iOS SDK

PS我实现了名称可能是针对某些copyrighting,其发展:)

enter image description here

+0

嗨@Giannis,你有没有找到解决你的问题?我和你的情况完全相同。 – TimothePearce

+2

没有使用教程代码,这是我用的: https://pastebin.com/yFKiqV2Z – Giannis

+0

嘿@Giannis,感谢您的片段!我正在与TimothePearce合作并尝试使用您的代码。我是Objective-C中的绝对虚拟人(他也是如此),并且在使用Spotify登录后无法获取回调。 Webview打开后,我显示了Spotify授权页面,但是当我回到应用程序时,什么也没有发生。我正在测试一个简单的console.log(令牌),它不起作用。我从你的代码中看到,ObjC回调应该将东西记录在调试器中(如@“authenticate”),但它也不会发生。你知道为什么吗? – rgehan

回答

3
只是临时

感谢您的提示(在评论中),我们设法使我们的Spotify验证与React-native一起工作。

我们使用来自您的Pastebin的代码来创建一个可重用的模块,这样就不需要浪费时间了。

你可以在这里找到模块:emphaz/react-native-ios-spotify-sdk

没有为建立一个教程,我们甚至创造了一个boilerplate project

非常感谢Giannis!

相关问题