Skip to content

Instantly share code, notes, and snippets.

@jackfreeman
Last active June 28, 2018 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackfreeman/8d2c10a7ec375097ef822612f1ca9079 to your computer and use it in GitHub Desktop.
Save jackfreeman/8d2c10a7ec375097ef822612f1ca9079 to your computer and use it in GitHub Desktop.
Spotify iOS App Remote AppDelegate Example
#import "AppDelegate.h"
#import <SpotifyAppRemote/SpotifyAppRemote.h>
@interface AppDelegate () <SPTAppRemoteDelegate, SPTAppRemotePlayerStateDelegate>
@property (nonatomic, strong) SPTAppRemote *appRemote;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
SPTAppRemoteConnectionParamsImageFormat format = SPTAppRemoteConnectionParamsImageFormatAny;
SPTAppRemoteConnectionParams *params =
[[SPTAppRemoteConnectionParams alloc] initWithClientIdentifier:@"your_client_id"
redirectURI:@"your_redirect_uri"
name:@"your_app_name"
accessToken:nil
defaultImageSize:CGSizeZero
imageFormat:format];
self.appRemote = [[SPTAppRemote alloc] initWithConnectionParameters:params logLevel:SPTAppRemoteLogLevelDebug];
self.appRemote.delegate = self;
// Dispatch to the next runloop so our app can finish launching
dispatch_async(dispatch_get_main_queue(), ^{
NSString *uri = @"spotify:track:69bp2EbF7Q2rqc5N3ylezZ";
BOOL spotifyInstalled = [self.appRemote authorizeAndPlayURI:uri];
if (!spotifyInstalled) {
/*
* The Spotify app is not installed.
* Use SKStoreProductViewController with [SPTAppRemote spotifyItunesItemIdentifier]
* to present the user with a way to install the Spotify app.
*/
}
});
return YES;
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
NSDictionary *params = [self.appRemote authorizationParametersFromURL:url];
NSString *token = params[SPTAppRemoteAccessTokenKey];
if (token) {
self.appRemote.connectionParameters.accessToken = token;
} else if (params[SPTAppRemoteErrorDescriptionKey]) {
NSLog(@"%@", params[SPTAppRemoteErrorDescriptionKey]);
}
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
if (self.appRemote.isConnected) {
[self.appRemote disconnect];
}
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
if (self.appRemote.connectionParameters.accessToken) {
[self.appRemote connect];
}
}
#pragma mark - SPTAppRemoteDelegate
- (void)appRemoteDidEstablishConnection:(SPTAppRemote *)appRemote
{
// Connection was successful, you can begin issuing commands
appRemote.playerAPI.delegate = self;
[appRemote.playerAPI subscribeToPlayerState:^(id _Nullable result, NSError * _Nullable error) {
if (error) {
NSLog(@"%@", error.localizedDescription);
}
}];
}
- (void)appRemote:(SPTAppRemote *)appRemote didFailConnectionAttemptWithError:(NSError *)error
{
// Connection failed
}
- (void)appRemote:(SPTAppRemote *)appRemote didDisconnectWithError:(nullable NSError *)error
{
// Connection disconnected
}
#pragma mark - SPTAppRemotePlayerStateDelegate
- (void)playerStateDidChange:(id<SPTAppRemotePlayerState>)playerState
{
NSLog(@"Track name: %@", playerState.track.name);
}
@end
@neohigor
Copy link

Hi Jack!
Could you provide the other files of Spotify iOS App Remote in Objective C?
Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment