我正在关注Spotify SDK tutorial,并尝试为我的应用程序制作RN模块。这是我的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
我想从RN端使用它的方式是调用身份验证,带有访问令牌的回调。我在Android上做得很好。
Native.authenticate(function(token) {
store.dispatch(actions.loginSuccess(token));
});
在iOS上,使用上面的代码,我进入了附加的屏幕,当点击“确定”时,我收到以下错误:
SpotiFind [5475:29641] ***因未捕获的异常而终止应用 'NSInvalidArgumentException',原因:'+ [SpotifyModule application:openURL:sourceApplication:annotation:]:unrecognized 选择器发送到类0x10cb406f8'
因此,从我最小的ObjectiveC理解,它试图调用一个不同的方法,而不是教程指示实现的方法。 关于如何使这项工作的任何建议?
如果它有任何相关性,我会针对iOS 10构建,并使用最新的Spotify iOS SDK
p.s我意识到这个名字可能反对某些版权,它只是发展的温度:)
答案 0 :(得分:3)
感谢您的提示(在评论中),我们设法使Spotify身份验证与React-native一起使用。
我们使用您的Pastebin中的代码创建一个可重复使用的模块,以便没有人不再浪费时间。
您可以在此处找到该模块:emphaz/react-native-ios-spotify-sdk
有一个设置教程,我们甚至创建了一个boilerplate project
非常感谢Giannis!