-
Notifications
You must be signed in to change notification settings - Fork 0
/
SignupViewController.m
107 lines (76 loc) · 3.83 KB
/
SignupViewController.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
//
// SignupViewController.m
// Ribbit
//
// Created by Андрей Балясников on 08/02/14.
// Copyright (c) 2014 Andrey Balyasnikov. All rights reserved.
//
#import "SignupViewController.h"
#import <Parse/Parse.h>
@interface SignupViewController ()
@end
@implementation SignupViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// добавляем padding
self.usernameField.leftView = [self paddingForTextField];
self.usernameField.leftViewMode = UITextFieldViewModeAlways;
self.passwordField.leftView = [self paddingForTextField];
self.passwordField.leftViewMode = UITextFieldViewModeAlways;
self.emailField.leftView = [self paddingForTextField];
self.emailField.leftViewMode = UITextFieldViewModeAlways;
}
- (IBAction)signup:(id)sender
{
// self.usernameField.text - просто засовываем текст в стринг
// stringByTrimmingCharactersInSet - обрезаем пустое пространсво (пробелы) с обоих
// концов строки и похоже - это проверка, чтобы не засирать бд
NSString *username = [self.usernameField.text stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *password = [self.passwordField.text stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *email = [self.emailField.text stringByTrimmingCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ( [username length] == 0 || [password length] == 0 || [email length] == 0 ) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops"
message:@"Make sure you enter a user name, password and email!"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
} else {
// создаем нового юзера в parse.com
// у экземпляра класса есть свойства, к ним приравниваем наши переменные с данными
// документация тут: https://parse.com/docs/ios_guide#users/iOS
PFUser *newUser = [PFUser user];
newUser.username = username;
newUser.password = password;
newUser.email = email;
// регистрируем юзера
[newUser signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
// обрабатываем ошибку
if (error) {
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"Sorry"
message:[error.userInfo objectForKey:@"error"]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alerView show];
} else {
// если все ок, то возвращаем юзера в корневой контроллер, в даном случае - инбокс
[self.navigationController popToRootViewControllerAnimated:YES];
}
}];
}
}
- (IBAction)dismiss:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - helper methods
- (UIView *) paddingForTextField
{
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 15, 44)];
}
@end