diff --git a/Simperium-OSX/SPAuthenticationTextField.h b/Simperium-OSX/SPAuthenticationTextField.h index c9c68fec..15e3c8dd 100644 --- a/Simperium-OSX/SPAuthenticationTextField.h +++ b/Simperium-OSX/SPAuthenticationTextField.h @@ -13,7 +13,8 @@ @property (assign) id delegate; @property (strong) NSTextField *textField; -- (id)initWithFrame:(NSRect)frame secure:(BOOL)secure; +- (instancetype)initWithFrame:(NSRect)frame secure:(BOOL)secure; + - (void)setPlaceholderString:(NSString *)string; - (NSString *)stringValue; - (void)setStringValue:(NSString *)string; diff --git a/Simperium-OSX/SPAuthenticationTextField.m b/Simperium-OSX/SPAuthenticationTextField.m index f495c2ec..00b905ef 100644 --- a/Simperium-OSX/SPAuthenticationTextField.m +++ b/Simperium-OSX/SPAuthenticationTextField.m @@ -55,12 +55,11 @@ - (BOOL)becomeFirstResponder { -#pragma mark ==================================================================================== -#pragma mark SPAuthenticationTextField -#pragma mark ==================================================================================== +#pragma mark - SPAuthenticationTextField @interface SPAuthenticationTextField() @property (nonatomic, assign) BOOL isWindowFistResponder; +@property (nonatomic, assign) BOOL secure; @end @implementation SPAuthenticationTextField @@ -72,34 +71,44 @@ - (void)dealloc { - (instancetype)initWithFrame:(NSRect)frame secure:(BOOL)secure { self = [super initWithFrame:frame]; if (self) { - // Center the textField vertically - int paddingX = 10; - int fontSize = 20; - CGFloat fieldHeight = [[SPAuthenticationConfiguration sharedInstance] regularFontHeightForSize:fontSize]; - CGFloat fieldY = (self.frame.size.height - fieldHeight) / 2; - CGRect textFrame = NSMakeRect(paddingX, fieldY, frame.size.width-paddingX*2, fieldHeight); - - Class textFieldClass = secure ? [SPSecureTextField class] : [SPTextField class]; - _textField = [[textFieldClass alloc] initWithFrame:textFrame]; - NSFont *font = [NSFont fontWithName:[SPAuthenticationConfiguration sharedInstance].regularFontName size:fontSize]; - [_textField setFont:font]; - [_textField setTextColor:[NSColor colorWithCalibratedWhite:0.1 alpha:1.0]]; - [_textField setDrawsBackground:NO]; - [_textField setBezeled:NO]; - [_textField setBordered:NO]; - [_textField setFocusRingType:NSFocusRingTypeNone]; - [[_textField cell] setWraps:NO]; - [[_textField cell] setScrollable:YES]; - [self addSubview:_textField]; - - NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; - [nc addObserver:self selector:@selector(handleTextFieldDidBeginEditing:) name:SPTextFieldDidBecomeFirstResponder object:_textField]; - [nc addObserver:self selector:@selector(handleTextFieldDidFinishEditing:) name:NSControlTextDidEndEditingNotification object:_textField]; + [self setupInterface:secure]; } - + return self; } +- (void)awakeFromNib { + [super awakeFromNib]; + [self setupInterface:self.secure]; +} + +- (void)setupInterface:(BOOL)secure { + // Center the textField vertically + NSRect frame = self.frame; + CGFloat paddingX = 10; + CGFloat fontSize = 20; + CGFloat fieldHeight = [[SPAuthenticationConfiguration sharedInstance] regularFontHeightForSize:fontSize]; + CGFloat fieldY = (self.frame.size.height - fieldHeight) / 2; + CGRect textFrame = NSMakeRect(paddingX, fieldY, frame.size.width-paddingX*2, fieldHeight); + + Class textFieldClass = secure ? [SPSecureTextField class] : [SPTextField class]; + _textField = [[textFieldClass alloc] initWithFrame:textFrame]; + NSFont *font = [NSFont fontWithName:[SPAuthenticationConfiguration sharedInstance].regularFontName size:fontSize]; + [_textField setFont:font]; + [_textField setTextColor:[NSColor colorWithCalibratedWhite:0.1 alpha:1.0]]; + [_textField setDrawsBackground:NO]; + [_textField setBezeled:NO]; + [_textField setBordered:NO]; + [_textField setFocusRingType:NSFocusRingTypeNone]; + [[_textField cell] setWraps:NO]; + [[_textField cell] setScrollable:YES]; + [self addSubview:_textField]; + + NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; + [nc addObserver:self selector:@selector(handleTextFieldDidBeginEditing:) name:SPTextFieldDidBecomeFirstResponder object:_textField]; + [nc addObserver:self selector:@selector(handleTextFieldDidFinishEditing:) name:NSControlTextDidEndEditingNotification object:_textField]; +} + - (void)setStringValue:(NSString *)string { _textField.stringValue = string; } @@ -128,11 +137,11 @@ - (void)setEnabled:(BOOL)enabled { - (void)drawRect:(NSRect)dirtyRect { NSBezierPath *betterBounds = [NSBezierPath bezierPathWithRect:self.bounds]; [betterBounds addClip]; - + if (self.isWindowFistResponder) { [[NSColor colorWithCalibratedWhite:0.9 alpha:1.0] setFill]; [betterBounds fill]; - + } else { [[NSColor colorWithCalibratedWhite:250.f/255.f alpha:1.0] setFill]; [betterBounds fill]; diff --git a/Simperium/SPAuthenticator.h b/Simperium/SPAuthenticator.h index 6bd85528..46f2ec75 100644 --- a/Simperium/SPAuthenticator.h +++ b/Simperium/SPAuthenticator.h @@ -43,6 +43,9 @@ NS_ASSUME_NONNULL_BEGIN - (BOOL)authenticateIfNecessary; +- (void)authenticateWithUsername:(NSString *)username + token:(NSString *)token; + - (void)authenticateWithUsername:(NSString *)username password:(NSString *)password success:(SuccessBlockType)successHandler diff --git a/Simperium/SPAuthenticator.m b/Simperium/SPAuthenticator.m index 01685753..9fb0dd4d 100644 --- a/Simperium/SPAuthenticator.m +++ b/Simperium/SPAuthenticator.m @@ -117,6 +117,19 @@ - (BOOL)authenticateIfNecessary { #pragma mark - Authentication +- (void)authenticateWithUsername:(NSString *)username + token:(NSString *)token { + NSParameterAssert(username); + NSParameterAssert(token); + + SPUser *user = [[SPUser alloc] initWithEmail:username token:token]; + self.simperium.user = user; + + [self saveCredentialsForUser:user]; + [self notifySignupDidSucceed]; + [self notifyAuthenticationDidSucceed]; +} + - (void)authenticateWithUsername:(NSString *)username password:(NSString *)password success:(SuccessBlockType)successHandler diff --git a/Simperium/SPEnvironment.m b/Simperium/SPEnvironment.m index acfac6fe..0d2c51d8 100644 --- a/Simperium/SPEnvironment.m +++ b/Simperium/SPEnvironment.m @@ -30,7 +30,7 @@ #endif // TODO: Update this automatically via a script that looks at current git tag -NSString* const SPLibraryVersion = @"1.4.0"; +NSString* const SPLibraryVersion = @"1.5.0"; /// SSL Pinning ///