ReactNative iOS Spotify SDK
ReactNative iOS Spotify SDK
我正在关注 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] *** 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
p.s 我意识到这个名字可能会反对某些版权,它只是开发的临时 :)
感谢您的提示(在评论中),我们设法使我们的 Spotify 身份验证与 React-native 一起工作。
我们使用您的 Pastebin 中的代码创建了一个可重复使用的模块,这样就无需再浪费时间了。
您可以在这里找到该模块:emphaz/react-native-ios-spotify-sdk
有一个安装教程,我们甚至创建了一个 boilerplate project
非常感谢扬尼斯!
我正在关注 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] *** 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
p.s 我意识到这个名字可能会反对某些版权,它只是开发的临时 :)
感谢您的提示(在评论中),我们设法使我们的 Spotify 身份验证与 React-native 一起工作。
我们使用您的 Pastebin 中的代码创建了一个可重复使用的模块,这样就无需再浪费时间了。
您可以在这里找到该模块:emphaz/react-native-ios-spotify-sdk
有一个安装教程,我们甚至创建了一个 boilerplate project
非常感谢扬尼斯!