2017-01-16 212 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!

相關問題