This repository was archived by the owner on Jan 16, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 285
/
Copy pathPFLogInViewController.m
601 lines (498 loc) · 22.3 KB
/
PFLogInViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*
* Copyright (c) 2014, Parse, LLC. All rights reserved.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
* copy, modify, and distribute this software in source code or binary form for use
* in connection with the web services and APIs provided by Parse.
*
* As with any software that integrates with the Parse platform, your use of
* this software is subject to the Parse Terms of Service
* [https://www.parse.com/about/terms]. This copyright notice shall be
* included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#import "PFLogInViewController.h"
#import <Parse/PFUser.h>
#import "PFActionButton.h"
#import "PFUIAlertView.h"
#import "PFLocalization.h"
#import "PFPrimaryButton.h"
#import "PFSignUpViewController.h"
#import "PFTextField.h"
#import "PFLogInView_Private.h"
NSString *const PFLogInSuccessNotification = @"com.parse.ui.login.success";
NSString *const PFLogInFailureNotification = @"com.parse.ui.login.failure";
NSString *const PFLogInCancelNotification = @"com.parse.ui.login.cancel";
/*!
This protocol exists so that we can weakly refer to messages to pass to PFFacebookUtils without
actually taking a dependency on the symbols.
*/
@protocol WeaklyReferencedFBUtils <NSObject>
// FBSDKv3
+ (void)logInWithPermissions:(NSArray *)permissions block:(PFUserResultBlock)block;
// FBSDKv4
+ (void)logInInBackgroundWithReadPermissions:(NSArray *)permissions block:(PFUserResultBlock)block;
+ (void)logInInBackgroundWithPublishPermissions:(NSArray *)permissions block:(PFUserResultBlock)block;
@end
@protocol WeaklyReferenceTwitterUtils <NSObject>
+ (void)logInWithBlock:(PFUserResultBlock)block;
@end
@interface PFLogInViewController () {
struct {
BOOL shouldBeginLogIn : YES;
BOOL didLogInUser : YES;
BOOL didFailToLogIn : YES;
BOOL didCancelLogIn : YES;
} _delegateExistingMethods;
}
@property (nonatomic, strong, readwrite) PFLogInView *logInView;
@property (nonatomic, assign) BOOL loading;
@property (nonatomic, assign) CGFloat visibleKeyboardHeight;
@end
@implementation PFLogInViewController
///--------------------------------------
#pragma mark - Init
///--------------------------------------
- (instancetype)init {
if (self = [super init]) {
[self _commonInit];
}
return self;
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
[self _commonInit];
}
return self;
}
- (instancetype)initWithCoder:(NSCoder *)decoder {
if (self = [super initWithCoder:decoder]) {
[self _commonInit];
}
return self;
}
- (void)_commonInit {
self.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
self.modalPresentationStyle = UIModalPresentationFormSheet;
_fields = PFLogInFieldsDefault;
if ([self respondsToSelector:@selector(automaticallyAdjustsScrollViewInsets)]) {
self.automaticallyAdjustsScrollViewInsets = NO;
}
}
///--------------------------------------
#pragma mark - Dealloc
///--------------------------------------
- (void)dealloc {
// Unregister from all notifications
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
///--------------------------------------
#pragma mark - UIViewController
///--------------------------------------
- (void)loadView {
_logInView = [[PFLogInView alloc] initWithFields:_fields];
[_logInView setPresentingViewController:self];
self.view = _logInView;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self setupHandlers];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self _registerForKeyboardNotifications];
if (self.navigationController &&
self.fields & PFLogInFieldsDismissButton) {
self.fields = self.fields & ~PFLogInFieldsDismissButton;
[_logInView.dismissButton removeFromSuperview];
}
}
- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return UIInterfaceOrientationMaskAll;
}
return UIInterfaceOrientationMaskPortrait;
}
///--------------------------------------
#pragma mark - PFLogInViewController
///--------------------------------------
- (PFLogInView *)logInView {
return (PFLogInView *)self.view; // self.view will call loadView if the view is nil
}
- (void)setEmailAsUsername:(BOOL)otherEmailAsUsername {
self.logInView.emailAsUsername = otherEmailAsUsername;
}
- (BOOL)emailAsUsername {
return self.logInView.emailAsUsername;
}
- (void)setFields:(PFLogInFields)fields {
if (_fields != fields) {
_fields = fields;
// Avoid force loading logInView
if (_logInView) {
_logInView.fields = fields;
}
}
}
- (void)setDelegate:(id<PFLogInViewControllerDelegate>)delegate {
if (self.delegate != delegate) {
_delegate = delegate;
_delegateExistingMethods.shouldBeginLogIn = [delegate respondsToSelector:@selector(logInViewController:
shouldBeginLogInWithUsername:
password:)];
_delegateExistingMethods.didLogInUser = [delegate respondsToSelector:@selector(logInViewController:
didLogInUser:)];
_delegateExistingMethods.didFailToLogIn = [delegate respondsToSelector:@selector(logInViewController:
didFailToLogInWithError:)];
_delegateExistingMethods.didCancelLogIn = [delegate
respondsToSelector:@selector(logInViewControllerDidCancelLogIn:)];
}
}
- (PFSignUpViewController *)signUpController {
if (!_signUpController) {
_signUpController = [[PFSignUpViewController alloc] init];
_signUpController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
_signUpController.emailAsUsername = self.emailAsUsername;
}
return _signUpController;
}
- (void)setLoading:(BOOL)loading {
if (self.loading != loading) {
_loading = loading;
_logInView.usernameField.enabled = !self.loading;
_logInView.passwordField.enabled = !self.loading;
_logInView.passwordForgottenButton.enabled = !self.loading;
_logInView.dismissButton.enabled = !self.loading;
}
}
///--------------------------------------
#pragma mark - UITextFieldDelegate
///--------------------------------------
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == _logInView.usernameField) {
[_logInView.passwordField becomeFirstResponder];
}
if (textField == _logInView.passwordField) {
[self _dismissKeyboard];
[self _loginAction];
}
return YES;
}
///--------------------------------------
#pragma mark - UIAlertViewDelegate
///--------------------------------------
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
if (buttonIndex != [alertView cancelButtonIndex]) {
NSString *email = [alertView textFieldAtIndex:0].text;
[self _requestPasswordResetWithEmail:email];
}
}
///--------------------------------------
#pragma mark - Private
///--------------------------------------
- (void)setupHandlers {
[_logInView.dismissButton addTarget:self
action:@selector(_dismissAction)
forControlEvents:UIControlEventTouchUpInside];
_logInView.usernameField.delegate = self;
_logInView.passwordField.delegate = self;
[_logInView.logInButton addTarget:self action:@selector(_loginAction) forControlEvents:UIControlEventTouchUpInside];
[_logInView.passwordForgottenButton addTarget:self
action:@selector(_forgotPasswordAction)
forControlEvents:UIControlEventTouchUpInside];
[_logInView.facebookButton addTarget:self
action:@selector(_loginWithFacebook)
forControlEvents:UIControlEventTouchUpInside];
[_logInView.twitterButton addTarget:self
action:@selector(_loginWithTwitter)
forControlEvents:UIControlEventTouchUpInside];
[_logInView.signUpButton addTarget:self
action:@selector(_signupAction)
forControlEvents:UIControlEventTouchUpInside];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(_dismissKeyboard)];
[_logInView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.cancelsTouchesInView = NO;
}
- (void)_dismissAction {
[self cancelLogIn];
// Normally the role of dismissing a modal controller lies on the presenting controller.
// Here we violate the principle so that the presenting modal log in controller is especially easy.
// Cons of this design is that it makes working with non-modally presented log in controller hard;
// but this concern is mitigated by the fact that navigationally presented controller should not have
// dismiss button.
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)_forgotPasswordAction PF_EXTENSION_UNAVAILABLE("") {
NSString *title = NSLocalizedString(@"Reset Password", @"Forgot password request title in PFLogInViewController");
NSString *message = NSLocalizedString(@"Please enter the email address for your account.",
@"Email request message in PFLogInViewController");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:self
cancelButtonTitle:NSLocalizedString(@"Cancel", @"Cancel")
otherButtonTitles:NSLocalizedString(@"OK", @"OK"), nil];
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *textField = [alertView textFieldAtIndex:0];
textField.placeholder = NSLocalizedString(@"Email", @"Email");
textField.keyboardType = UIKeyboardTypeEmailAddress;
textField.returnKeyType = UIReturnKeyDone;
[alertView show];
}
- (void)_requestPasswordResetWithEmail:(NSString *)email {
[PFUser requestPasswordResetForEmailInBackground:email block:^(BOOL success, NSError *error) {
if (success) {
NSString *title = NSLocalizedString(@"Password Reset",
@"Password reset success alert title in PFLogInViewController.");
NSString *message = [NSString stringWithFormat:NSLocalizedString(@"An email with reset instructions has been sent to '%@'.",
@"Password reset message in PFLogInViewController"), email];
[PFUIAlertView showAlertViewWithTitle:title
message:message
cancelButtonTitle:NSLocalizedString(@"OK", @"OK")];
} else {
NSString *title = NSLocalizedString(@"Password Reset Failed",
@"Password reset error alert title in PFLogInViewController.");
[PFUIAlertView showAlertViewWithTitle:title error:error];
}
}];
}
#pragma mark Log In With Facebook
- (void)_loginWithFacebook {
if (self.loading) {
return;
}
self.loading = YES;
if ([_logInView.facebookButton isKindOfClass:[PFActionButton class]]) {
[(PFActionButton *)_logInView.facebookButton setLoading:YES];
}
__weak typeof(self) wself = self;
PFUserResultBlock resultBlock = ^(PFUser *user, NSError *error) {
__strong typeof(wself) sself = wself;
sself.loading = NO;
if ([_logInView.facebookButton isKindOfClass:[PFActionButton class]]) {
[(PFActionButton *)_logInView.facebookButton setLoading:NO];
}
if (user) {
[sself _loginDidSucceedWithUser:user];
} else if (error) {
[sself _loginDidFailWithError:error];
} else {
// User cancelled login.
}
};
Class fbUtils = NSClassFromString(@"PFFacebookUtils");
if ([fbUtils respondsToSelector:@selector(logInWithPermissions:block:)]) {
// Facebook SDK v3 Login
[fbUtils logInWithPermissions:_facebookPermissions block:resultBlock];
} else if ([fbUtils respondsToSelector:@selector(logInInBackgroundWithReadPermissions:block:)]) {
// Facebook SDK v4 Login
if ([self _permissionsContainsFacebookPublishPermission:_facebookPermissions]) {
[fbUtils logInInBackgroundWithPublishPermissions:_facebookPermissions block:resultBlock];
} else {
[fbUtils logInInBackgroundWithReadPermissions:_facebookPermissions block:resultBlock];
}
} else {
[NSException raise:NSInternalInconsistencyException
format:@"Can't find PFFacebookUtils. Please link with ParseFacebookUtils or ParseFacebookUtilsV4 to enable login with Facebook."];
}
}
- (BOOL)_permissionsContainsFacebookPublishPermission:(NSArray *)permissions {
for (NSString *permission in permissions) {
if ([permission hasPrefix:@"publish"] ||
[permission hasPrefix:@"manage"] ||
[permission isEqualToString:@"ads_management"] ||
[permission isEqualToString:@"create_event"] ||
[permission isEqualToString:@"rsvp_event"]) {
return YES;
}
}
return NO;
}
#pragma mark Log In With Twitter
- (void)_loginWithTwitter {
if (self.loading) {
return;
}
if ([_logInView.facebookButton isKindOfClass:[PFActionButton class]]) {
[(PFActionButton *)_logInView.twitterButton setLoading:YES];
}
self.loading = YES;
Class twitterUtils = NSClassFromString(@"PFTwitterUtils");
if (twitterUtils && [twitterUtils respondsToSelector:@selector(logInWithBlock:)]) {
[twitterUtils logInWithBlock:^(PFUser *user, NSError *error) {
self.loading = NO;
if ([_logInView.facebookButton isKindOfClass:[PFActionButton class]]) {
[(PFActionButton *)_logInView.twitterButton setLoading:NO];
}
if (user) {
[self _loginDidSucceedWithUser:user];
} else if (error) {
[self _loginDidFailWithError:error];
} else {
// User cancelled login.
}
}];
} else {
[NSException raise:NSInternalInconsistencyException
format:@"Can't find PFTwitterUtils. Please link with ParseTwitterUtils to enable login with Twitter."];
}
}
#pragma mark Log In
- (void)_loginAction {
if (self.loading) {
return;
}
NSString *username = _logInView.usernameField.text ?: @"";
NSString *password = _logInView.passwordField.text ?: @"";
if (_delegateExistingMethods.shouldBeginLogIn) {
if (![_delegate logInViewController:self shouldBeginLogInWithUsername:username password:password]) {
return;
}
}
self.loading = YES;
if ([_logInView.logInButton isKindOfClass:[PFPrimaryButton class]]) {
[(PFActionButton *)_logInView.logInButton setLoading:YES];
}
[PFUser logInWithUsernameInBackground:username password:password block:^(PFUser *user, NSError *error) {
self.loading = NO;
if ([_logInView.logInButton isKindOfClass:[PFPrimaryButton class]]) {
[(PFPrimaryButton *)_logInView.logInButton setLoading:NO];
}
if (user) {
[self _loginDidSucceedWithUser:user];
} else {
[self _loginDidFailWithError:error];
}
}];
}
- (void)_signupAction {
if (self.loading) {
return;
}
[self presentViewController:self.signUpController animated:YES completion:nil];
}
- (void)_loginDidSucceedWithUser:(PFUser *)user {
if (_delegateExistingMethods.didLogInUser) {
[_delegate logInViewController:self didLogInUser:user];
}
[[NSNotificationCenter defaultCenter] postNotificationName:PFLogInSuccessNotification object:self];
}
- (void)_loginDidFailWithError:(NSError *)error {
if (_delegateExistingMethods.didFailToLogIn) {
[_delegate logInViewController:self didFailToLogInWithError:error];
} else {
NSString *title = NSLocalizedString(@"Login Failed", @"Login failed alert title in PFLogInViewController");
NSString *message = nil;
if (error.code == kPFErrorObjectNotFound) {
message = NSLocalizedString(@"The username and password you entered don't match", @"Invalid login credentials alert message in PFLogInViewController");
} else {
message = NSLocalizedString(@"Please try again", @"Generic login failed alert message in PFLogInViewController");
}
[PFUIAlertView showAlertViewWithTitle:title message:message];
}
[[NSNotificationCenter defaultCenter] postNotificationName:PFLogInFailureNotification object:self];
}
- (void)cancelLogIn {
if (_delegateExistingMethods.didCancelLogIn) {
[_delegate logInViewControllerDidCancelLogIn:self];
}
[[NSNotificationCenter defaultCenter] postNotificationName:PFLogInCancelNotification object:self];
}
///--------------------------------------
#pragma mark - Keyboard
///--------------------------------------
- (UIView *)currentFirstResponder {
if ([_logInView.usernameField isFirstResponder]) {
return _logInView.usernameField;
}
if ([_logInView.passwordField isFirstResponder]) {
return _logInView.passwordField;
}
return nil;
}
- (void)_dismissKeyboard {
[self.view endEditing:YES];
}
- (void)_registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)_keyboardWillShow:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
CGRect keyboardFrame = [self.view convertRect:endFrame fromView:self.view.window];
CGFloat visibleKeyboardHeight = CGRectGetMaxY(self.view.bounds) - CGRectGetMinY(keyboardFrame);
[self setVisibleKeyboardHeight:visibleKeyboardHeight
animationDuration:duration
animationOptions:curve << 16];
}
- (void)_keyboardWillHide:(NSNotification *)notification {
NSDictionary *userInfo = [notification userInfo];
CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
[self setVisibleKeyboardHeight:0.0
animationDuration:duration
animationOptions:curve << 16];
}
- (void)setVisibleKeyboardHeight:(CGFloat)visibleKeyboardHeight
animationDuration:(NSTimeInterval)animationDuration
animationOptions:(UIViewAnimationOptions)animationOptions {
dispatch_block_t animationsBlock = ^{
self.visibleKeyboardHeight = visibleKeyboardHeight;
};
if (animationDuration == 0.0) {
animationsBlock();
} else {
[UIView animateWithDuration:animationDuration
delay:0.0
options:animationOptions | UIViewAnimationOptionBeginFromCurrentState
animations:animationsBlock
completion:nil];
}
}
- (void)setVisibleKeyboardHeight:(CGFloat)visibleKeyboardHeight {
if (self.visibleKeyboardHeight != visibleKeyboardHeight) {
_visibleKeyboardHeight = visibleKeyboardHeight;
[self _updateViewContentOffsetAnimated:NO];
}
}
- (void)_updateViewContentOffsetAnimated:(BOOL)animated {
CGPoint contentOffset = CGPointZero;
if (self.visibleKeyboardHeight > 0.0f) {
// Scroll the view to keep fields visible
CGFloat offsetForScrollingTextFieldToTop = CGRectGetMinY([self currentFirstResponder].frame);
UIView *lowestView;
if (_logInView.passwordForgottenButton) {
lowestView = _logInView.passwordForgottenButton;
} else if (_logInView.logInButton) {
lowestView = _logInView.logInButton;
} else {
lowestView = _logInView.passwordField;
}
CGFloat offsetForScrollingLowestViewToBottom = 0.0f;
offsetForScrollingLowestViewToBottom += self.visibleKeyboardHeight;
offsetForScrollingLowestViewToBottom += CGRectGetMaxY(lowestView.frame);
offsetForScrollingLowestViewToBottom -= CGRectGetHeight(_logInView.bounds);
if (offsetForScrollingLowestViewToBottom < 0) {
return; // No scrolling required
}
contentOffset = CGPointMake(0.0f, MIN(offsetForScrollingTextFieldToTop, offsetForScrollingLowestViewToBottom));
}
[_logInView setContentOffset:contentOffset animated:animated];
}
@end