Skip to content

Instantly share code, notes, and snippets.

@shto
Created August 27, 2014 10:14
Show Gist options
  • Save shto/48715fd5da4d54005c51 to your computer and use it in GitHub Desktop.
Save shto/48715fd5da4d54005c51 to your computer and use it in GitHub Desktop.
Spotify Login Helper
//
// JBSpotifyLoginViewController.h
// JukeBox
//
// Created by Andrei on 9/2/13.
// Copyright (c) 2013 andreipatru. All rights reserved.
//
#import "SPLoginViewController.h"
@interface JBSpotifyLoginViewController : UIViewController <SPLoginViewControllerDelegate, SPSessionDelegate> {
}
@property (nonatomic, strong) SPLoginViewController *viewControllerSPLogin;
@property (nonatomic, strong) void (^successBlock)(SPSession *);
@property (nonatomic, strong) void (^failureBlock)(SPSession *, NSError *);
@property (nonatomic, strong) UIViewController *viewControllerCallingParent;
+ (BOOL)isLoggedIn;
- (id)init;
- (void)performLoginProcessFromViewController:(UIViewController *)modalParent
withSuccessBlock:(void (^)(SPSession *session))theSuccessBlock
andFailureBlock:(void (^)(SPSession *session, NSError *error))theFailBlock;
@end
//
// JBSpotifyLoginViewController.m
// JukeBox
//
// Created by Andrei on 9/2/13.
// Copyright (c) 2013 andreipatru. All rights reserved.
//
#import <Parse/Parse.h>
#import "JBSpotifyLoginViewController.h"
@interface JBSpotifyLoginViewController ()
@end
@implementation JBSpotifyLoginViewController
- (id)init {
self = [super init];
if (self) {
self.viewControllerSPLogin = [SPLoginViewController loginControllerForSession:[SPSession sharedSession]];
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
self.viewControllerSPLogin.edgesForExtendedLayout = UIRectEdgeNone;
}
self.viewControllerSPLogin.loginDelegate = self;
self.viewControllerSPLogin.dismissesAfterLogin = YES;
[[SPSession sharedSession] setDelegate:self];
}
return self;
}
+ (BOOL)isLoggedIn {
return ([SPSession sharedSession].connectionState == SP_CONNECTION_STATE_LOGGED_IN);
}
- (void)performLoginProcessFromViewController:(UIViewController *)modalParent
withSuccessBlock:(void (^)(SPSession *session))theSuccessBlock
andFailureBlock:(void (^)(SPSession *session, NSError *error))theFailBlock;
{
self.viewControllerCallingParent = modalParent;
self.successBlock = theSuccessBlock;
self.failureBlock = theFailBlock;
if ([JBSpotifyLoginViewController isLoggedIn]) {
// a user is already logged in
self.successBlock([SPSession sharedSession]);
} else {
// not logged in
[modalParent presentViewController:self.viewControllerSPLogin
animated:YES
completion:nil];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
if ([self respondsToSelector:@selector(edgesForExtendedLayout)]) {
self.edgesForExtendedLayout = UIRectEdgeNone;
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - SPLoginViewControllerDelegate
/** Called when the login/signup process has completed.
@param controller The SPLoginViewController instance that generated the message.
@param didLogin `YES` if the user successfully logged in, otherwise `NO`..
*/
-(void)loginViewController:(SPLoginViewController *)controller didCompleteSuccessfully:(BOOL)didLogin {
if (controller == self.viewControllerSPLogin) {
}
}
#pragma mark - SPSessionDelegate
/** Called when a set of login credentials has been generated for the user, typically just after login.
If you wish to store login credentials for multiple users, store the credentials given by this
delegate method rather than their passwords (it's against the libSpotify Terms and Conditions to store
Spotify passwords yourself). These credentials are safe to store without encryption, such as in `NSUserDefaults`.
To use these credentials to log in, use `-attemptLoginWithUserName:existingCredential:rememberCredentials:`.
@param aSession The session that logged in.
@param credential The login credential.
@param userName The username for the given credential.
*/
-(void)session:(SPSession *)aSession didGenerateLoginCredentials:(NSString *)credential forUserName:(NSString *)userName {
// TODO: could be a good idea to save userName and credential in NSUserDefaults here
}
/** Called when the given session has logged in successfully.
@param aSession The session that logged in.
*/
-(void)sessionDidLoginSuccessfully:(SPSession *)aSession {
self.successBlock(aSession);
if (self.viewControllerSPLogin.dismissesAfterLogin) {
[self.viewControllerCallingParent dismissViewControllerAnimated:YES
completion:^{
}];
}
}
/** Called when the given session could not log in successfully.
@param aSession The session that failed to log in.
@param error An NSError object describing the failure.
*/
-(void)session:(SPSession *)aSession didFailToLoginWithError:(NSError *)error {
self.failureBlock(aSession, error);
}
@end
// ...
JBSpotifyLoginViewController *loginController = [[JBSpotifyLoginViewController alloc] init];
[loginController
performLoginProcessFromViewController:self
withSuccessBlock:^(SPSession *session) {
// do stuff
} andFailureBlock:^(SPSession *session, NSError *error) {
NSLog(@"failed to log in");
}];
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment