gpt4 book ai didi

ios - 响应 native iOS Spotify SDK

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:54:54 24 4
gpt4 key购买 nike

我正在关注 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 端使用它的方式是调用身份验证,并回调访问 token 。我在 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 我意识到这个名字可能会反对某些版权,它只是开发的临时 :)

enter image description here

最佳答案

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

我们使用您的 Pastebin 中的代码创建了一个可重复使用的模块,这样就无需再浪费时间了。

您可以在此处找到该模块:emphaz/react-native-ios-spotify-sdk

有一个设置教程,我们甚至创建了一个 boilerplate project

非常感谢扬尼斯!

关于ios - 响应 native iOS Spotify SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41682991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com