From b50303b97d2ca5389db38e585f0675f90e7dcaef Mon Sep 17 00:00:00 2001 From: phongnv Date: Wed, 2 Aug 2017 21:33:44 +0700 Subject: [PATCH] update api --- LifeLog/LifeLog.xcodeproj/project.pbxproj | 18 +++++ LifeLog/LifeLog/LoginViewController.m | 44 ++++++----- LifeLog/LifeLog/RegisterViewController.m | 123 +++++++++++++++++++++--------- LifeLog/LifeLog/ServerAPI.h | 4 +- LifeLog/LifeLog/ServerAPI.m | 84 +++++++++++++++++--- LifeLog/LifeLog/User.m | 58 +++++++++----- LifeLog/LifeLog/Utilities.h | 1 + LifeLog/LifeLog/Utilities.m | 23 ++++++ 8 files changed, 270 insertions(+), 85 deletions(-) diff --git a/LifeLog/LifeLog.xcodeproj/project.pbxproj b/LifeLog/LifeLog.xcodeproj/project.pbxproj index 81d3ac8..760e965 100644 --- a/LifeLog/LifeLog.xcodeproj/project.pbxproj +++ b/LifeLog/LifeLog.xcodeproj/project.pbxproj @@ -35,6 +35,8 @@ 6EDC8BEE1F2C2FB800765ACC /* Utilities.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EDC8BED1F2C2FB800765ACC /* Utilities.m */; }; 6EDC8BF41F2C68D700765ACC /* CMMotionActivityExtra.m in Sources */ = {isa = PBXBuildFile; fileRef = 6EDC8BF31F2C68D700765ACC /* CMMotionActivityExtra.m */; }; 6EF1630A1F29069300DA306C /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 6EF163091F29069300DA306C /* Localizable.strings */; }; + 9CA8AEEA1F31BB4600C5FC74 /* ConfirmForgetPassViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CA8AEE81F31BB4600C5FC74 /* ConfirmForgetPassViewController.m */; }; + 9CA8AEEB1F31BB4600C5FC74 /* ConfirmForgetPassViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9CA8AEE91F31BB4600C5FC74 /* ConfirmForgetPassViewController.xib */; }; 9CBDA0FA1F2ECE2C0055DED1 /* BaseViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CBDA0F91F2ECE2C0055DED1 /* BaseViewController.m */; }; 9CBDA0FF1F2F03810055DED1 /* RegisterViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 9CBDA0FD1F2F03810055DED1 /* RegisterViewController.m */; }; 9CBDA1001F2F03810055DED1 /* RegisterViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 9CBDA0FE1F2F03810055DED1 /* RegisterViewController.xib */; }; @@ -109,6 +111,9 @@ 6EDC8BF21F2C68D700765ACC /* CMMotionActivityExtra.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CMMotionActivityExtra.h; sourceTree = ""; }; 6EDC8BF31F2C68D700765ACC /* CMMotionActivityExtra.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CMMotionActivityExtra.m; sourceTree = ""; }; 6EF163091F29069300DA306C /* Localizable.strings */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; path = Localizable.strings; sourceTree = ""; }; + 9CA8AEE71F31BB4600C5FC74 /* ConfirmForgetPassViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ConfirmForgetPassViewController.h; sourceTree = ""; }; + 9CA8AEE81F31BB4600C5FC74 /* ConfirmForgetPassViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ConfirmForgetPassViewController.m; sourceTree = ""; }; + 9CA8AEE91F31BB4600C5FC74 /* ConfirmForgetPassViewController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = ConfirmForgetPassViewController.xib; sourceTree = ""; }; 9CBDA0F81F2ECE2C0055DED1 /* BaseViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BaseViewController.h; sourceTree = ""; }; 9CBDA0F91F2ECE2C0055DED1 /* BaseViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BaseViewController.m; sourceTree = ""; }; 9CBDA0FC1F2F03810055DED1 /* RegisterViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RegisterViewController.h; sourceTree = ""; }; @@ -153,6 +158,7 @@ 6E18CECF1F2E398C0029891F /* Login */ = { isa = PBXGroup; children = ( + 9CA8AEE61F31BB1200C5FC74 /* ConfirmForgetPass */, 6E18CED01F2E39DB0029891F /* LoginViewController.h */, 6E18CED11F2E39DB0029891F /* LoginViewController.m */, 6E18CED21F2E39DB0029891F /* LoginViewController.xib */, @@ -358,6 +364,16 @@ name = Common; sourceTree = ""; }; + 9CA8AEE61F31BB1200C5FC74 /* ConfirmForgetPass */ = { + isa = PBXGroup; + children = ( + 9CA8AEE71F31BB4600C5FC74 /* ConfirmForgetPassViewController.h */, + 9CA8AEE81F31BB4600C5FC74 /* ConfirmForgetPassViewController.m */, + 9CA8AEE91F31BB4600C5FC74 /* ConfirmForgetPassViewController.xib */, + ); + name = ConfirmForgetPass; + sourceTree = ""; + }; 9CBDA0F71F2ECDFD0055DED1 /* BaseViewController */ = { isa = PBXGroup; children = ( @@ -501,6 +517,7 @@ 9CBDA1001F2F03810055DED1 /* RegisterViewController.xib in Resources */, 6E18CED41F2E39DB0029891F /* LoginViewController.xib in Resources */, 6E84E3BB1F27A9A0001EB88E /* RankingViewController.xib in Resources */, + 9CA8AEEB1F31BB4600C5FC74 /* ConfirmForgetPassViewController.xib in Resources */, 6E84E3C01F27A9B1001EB88E /* SNSViewController.xib in Resources */, 6E8DA0F81F2BAC3500CCE941 /* TodayViewController.xib in Resources */, 6E84E3841F27A701001EB88E /* LaunchScreen.storyboard in Resources */, @@ -590,6 +607,7 @@ 6E84E3BA1F27A9A0001EB88E /* RankingViewController.m in Sources */, 6EDC8BF41F2C68D700765ACC /* CMMotionActivityExtra.m in Sources */, 9CBDA0FF1F2F03810055DED1 /* RegisterViewController.m in Sources */, + 9CA8AEEA1F31BB4600C5FC74 /* ConfirmForgetPassViewController.m in Sources */, 6EDC8BEE1F2C2FB800765ACC /* Utilities.m in Sources */, 6E84E3C51F27ACAE001EB88E /* MapViewController.m in Sources */, 6E18CEDC1F2E40F50029891F /* User.m in Sources */, diff --git a/LifeLog/LifeLog/LoginViewController.m b/LifeLog/LifeLog/LoginViewController.m index 7e5de0d..c20d044 100644 --- a/LifeLog/LifeLog/LoginViewController.m +++ b/LifeLog/LifeLog/LoginViewController.m @@ -10,6 +10,8 @@ #import "ServerAPI.h" #import "AppDelegate.h" #import "RegisterViewController.h" +#import "Utilities.h" +#import "ConfirmForgetPassViewController.h" @interface LoginViewController () @property (nonatomic, weak) IBOutlet UILabel *lblEmail; @@ -64,31 +66,35 @@ else { dispatch_async(dispatch_get_main_queue(), ^{ NSString *message = [error.userInfo objectForKey:@"message"]; - if (message.length > 0) { - UIAlertController * alert= [UIAlertController - alertControllerWithTitle:@"Error" - message:message - preferredStyle:UIAlertControllerStyleAlert]; - - UIAlertAction* ok = [UIAlertAction - actionWithTitle:@"OK" - style:UIAlertActionStyleDefault - handler:^(UIAlertAction * action) - { - [alert dismissViewControllerAnimated:YES completion:nil]; - - }]; - - [alert addAction:ok]; - - [weakSelf presentViewController:alert animated:YES completion:nil]; - } + [Utilities showErrorMessage:message withViewController:weakSelf]; }); } }]; } - (IBAction)buttonForgetTouchUpInside:(id)sender { + if (_tfEmail.text.length > 0) { + LoginViewController __weak *weakSelf = self; + [[ServerAPI server] forgetPass:_tfEmail.text CompletionHandler:^(NSError *error) { + if (error == nil) { + // goto screen confirm forgetPass + dispatch_async(dispatch_get_main_queue(), ^{ + ConfirmForgetPassViewController *confirmVC = [[ConfirmForgetPassViewController alloc] initWithNibName:@"ConfirmForgetPassViewController" bundle:nil]; + confirmVC.email = weakSelf.tfEmail.text; + [weakSelf.navigationController pushViewController:confirmVC animated:YES]; + }); + } + else { + dispatch_async(dispatch_get_main_queue(), ^{ + NSString *message = [error.userInfo objectForKey:@"message"]; + [Utilities showErrorMessage:message withViewController:weakSelf]; + }); + } + }]; + } + else { + [Utilities showErrorMessage:@"Please input email" withViewController:self]; + } } - (IBAction)buttonRegisterTouchUpInside:(id)sender { diff --git a/LifeLog/LifeLog/RegisterViewController.m b/LifeLog/LifeLog/RegisterViewController.m index 3247ca8..033b991 100644 --- a/LifeLog/LifeLog/RegisterViewController.m +++ b/LifeLog/LifeLog/RegisterViewController.m @@ -160,8 +160,9 @@ static int const kPickerTagSex = 2; } - (IBAction)registButtonPressed:(id)sender { - NSLog(@"registButtonPressed"); - [self registerUser]; + if ([self checkAllowRegister]) { + [self registerUser]; + } } #pragma mark - UIActionSheetDelegate @@ -203,7 +204,7 @@ static int const kPickerTagSex = 2; else { dispatch_async(dispatch_get_main_queue(), ^{ NSString *message = [error.userInfo objectForKey:@"message"]; - [weakSelf showErrorMessage:message]; + [Utilities showErrorMessage:message withViewController:weakSelf]; }); } }]; @@ -413,6 +414,67 @@ static int const kPickerTagSex = 2; return dict; } +- (BOOL)checkAllowRegister +{ + NSString *message = @""; + if (_tfUsername.text.length <= 0) { + message = @"Please input username"; + [Utilities showErrorMessage:message withViewController:self]; + return NO; + } + if (_tfName.text.length <= 0) { + message = @"Please input full name"; + [Utilities showErrorMessage:message withViewController:self]; + return NO; + } + if (_tfEmail.text.length <= 0) { + message = @"Please input email"; + [Utilities showErrorMessage:message withViewController:self]; + return NO; + } + if (_tfPassword.text.length <= 0) { + message = @"Please input password"; + [Utilities showErrorMessage:message withViewController:self]; + return NO; + } + if (_tfConfirmPassword.text.length <= 0) { + message = @"Please input confirm password"; + [Utilities showErrorMessage:message withViewController:self]; + return NO; + } + if (_tflblSex.text.length <= 0) { + message = @"Please input gender"; + [Utilities showErrorMessage:message withViewController:self]; + return NO; + } + if (_tfUsername.text.length > 100) { + message = @"Username maximum length 100"; + [Utilities showErrorMessage:message withViewController:self]; + return NO; + } + if (_tfName.text.length > 255) { + message = @"Full name maximum length 255"; + [Utilities showErrorMessage:message withViewController:self]; + return NO; + } + if (_tfEmail.text.length > 255) { + message = @"Email maximum length 255"; + [Utilities showErrorMessage:message withViewController:self]; + return NO; + } + if (_tfPassword.text.length < 6) { + message = @"Password minimum length 6"; + [Utilities showErrorMessage:message withViewController:self]; + return NO; + } + if (![_tfEmail.text containsString:@"@"]) { + message = @"Email invalid"; + [Utilities showErrorMessage:message withViewController:self]; + return NO; + } + return YES; +} + - (NSDictionary *)createParamsForRegisterUser { NSMutableDictionary *dictResutl = [[NSMutableDictionary alloc] init]; dictResutl = [self addParam:@"username" value:_tfUsername.text withDict:dictResutl]; @@ -420,20 +482,24 @@ static int const kPickerTagSex = 2; dictResutl = [self addParam:@"nickname" value:_tfNickname.text withDict:dictResutl]; dictResutl = [self addParam:@"email" value:_tfEmail.text withDict:dictResutl]; dictResutl = [self addParam:@"password" value:_tfPassword.text withDict:dictResutl]; - //dictResutl = [self addParam:@"confirm_password" value:_tfConfirmPassword.text withDict:dictResutl]; - NSString *valueSex = nil; // 0 - 男, 1 - 女 + dictResutl = [self addParam:@"password_confirmation" value:_tfConfirmPassword.text withDict:dictResutl]; + // 0 - 男, 1 - 女 // NSString *valueSex = nil; if (_tflblSex.text.length > 0) { if ([_tflblSex.text isEqualToString:@"男"]) { - valueSex = @"0"; + [dictResutl setValue:[NSNumber numberWithInt:0] forKey:@"gender"]; } else if ([_tflblSex.text isEqualToString:@"女"]) { - valueSex = @"1"; + [dictResutl setValue:[NSNumber numberWithInt:1] forKey:@"gender"]; } } - dictResutl = [self addParam:@"gender" value:valueSex withDict:dictResutl]; + //dictResutl = [self addParam:@"gender" value:valueSex withDict:dictResutl]; NSString *valueBirthDay = nil; if (_tfBirthday.text.length > 0) { - valueBirthDay = [NSString stringWithFormat:@"%ld-%ld-%ld", (long)[dateBirthDay getYear], (long)[dateBirthDay getMonth], (long)[dateBirthDay getDay]]; + NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; + [dateFormatter setDateFormat:@"yyyy-MM-dd"]; + valueBirthDay = [dateFormatter stringFromDate:dateBirthDay]; + //valueBirthDay = [NSString stringWithFormat:@"%ld-%ld-%ld", (long)[dateBirthDay getYear], (long)[dateBirthDay getMonth], (long)[dateBirthDay getDay]]; + dictResutl = [self addParam:@"birthday" value:valueBirthDay withDict:dictResutl]; } dictResutl = [self addParam:@"height" value:_tfHeight.text withDict:dictResutl]; dictResutl = [self addParam:@"weight" value:_tfWeight.text withDict:dictResutl]; @@ -445,13 +511,25 @@ static int const kPickerTagSex = 2; if (![profile_image isEqualToString:@""]) { dictResutl = [self addParam:@"profile_image" value:profile_image withDict:dictResutl]; } + // hardcode + // require + [dictResutl setValue:[NSNumber numberWithInt:0] forKey:@"receive_notification"]; + // = [self addParam:@"receive_notification" value:@"0" withDict:dictResutl]; + [dictResutl setValue:[NSNumber numberWithInt:0] forKey:@"share_data"]; + // dictResutl = [self addParam:@"share_data" value:@"0" withDict:dictResutl]; + + // Option + //dictResutl = [self addParam:@"phone" value:@"01676582596" withDict:dictResutl]; + [dictResutl setValue:[NSNumber numberWithInt:0] forKey:@"remember"]; + //dictResutl = [self addParam:@"remember" value:@"0" withDict:dictResutl]; + return dictResutl; } - (void)registerUser { NSDictionary *paramRegister = [self createParamsForRegisterUser]; RegisterViewController __weak *weakSelf = self; - [[ServerAPI server] registerUserWithParams:paramRegister CompletionHandler:^(NSError *error) { + [[ServerAPI server] registerUserWithParams:paramRegister CompletionHandler:^(User *user, NSString *token, NSError *error) { if (weakSelf == nil) { return ; } @@ -463,33 +541,10 @@ static int const kPickerTagSex = 2; else { dispatch_async(dispatch_get_main_queue(), ^{ NSString *message = [error.userInfo objectForKey:@"message"]; - [weakSelf showErrorMessage:message]; + [Utilities showErrorMessage:message withViewController:weakSelf]; }); } }]; } -- (void)showErrorMessage:(NSString *)message -{ - if (message.length > 0) { - UIAlertController * alert= [UIAlertController - alertControllerWithTitle:@"Error" - message:message - preferredStyle:UIAlertControllerStyleAlert]; - - UIAlertAction* ok = [UIAlertAction - actionWithTitle:@"OK" - style:UIAlertActionStyleDefault - handler:^(UIAlertAction * action) - { - [alert dismissViewControllerAnimated:YES completion:nil]; - - }]; - - [alert addAction:ok]; - - [self presentViewController:alert animated:YES completion:nil]; - } -} - @end diff --git a/LifeLog/LifeLog/ServerAPI.h b/LifeLog/LifeLog/ServerAPI.h index be409a0..6c870ed 100644 --- a/LifeLog/LifeLog/ServerAPI.h +++ b/LifeLog/LifeLog/ServerAPI.h @@ -13,6 +13,8 @@ + (instancetype) server; @property (nonatomic, assign) NSTimeInterval timeOutInterval; - (void)loginWithEmail:(NSString *)email Password:(NSString *)password CompletionHandler: (void (^)(User *, NSString *, NSError *)) completion; -- (void)registerUserWithParams:(NSDictionary *)params CompletionHandler: (void (^)(NSError *)) completion; +- (void)registerUserWithParams:(NSDictionary *)params CompletionHandler: (void (^)(User *, NSString *, NSError *)) completion; - (void)uploadImage:(NSString *)token andImageData:(NSData *)data CompletionHandler:(void (^)(NSString *, NSError *)) completion; +- (void)forgetPass:(NSString *)email CompletionHandler:(void (^)(NSError *)) completion; +- (void)confirmForgetPass:(NSString *)email withConfirm:(NSString *)confirm CompletionHandler:(void (^)(NSError *)) completion; @end diff --git a/LifeLog/LifeLog/ServerAPI.m b/LifeLog/LifeLog/ServerAPI.m index 810b2cb..578d5b7 100644 --- a/LifeLog/LifeLog/ServerAPI.m +++ b/LifeLog/LifeLog/ServerAPI.m @@ -68,8 +68,7 @@ static ServerAPI *_server = nil; if (error == nil) { NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; - - + int status = [dataResult[@"status"] intValue]; if (status == 1) { // status = 1 success NSString *token = dataResult[@"result"][@"token"]; @@ -103,7 +102,7 @@ static ServerAPI *_server = nil; } // Register -- (void)registerUserWithParams:(NSDictionary *)params CompletionHandler: (void (^)(NSError *)) completion { +- (void)registerUserWithParams:(NSDictionary *)params CompletionHandler: (void (^)(User *, NSString *, NSError *)) completion { [self _request:[kServerAddress stringByAppendingFormat: @"register"] method:@"POST" paras:params completion:^(NSData *data, NSError *error) { if (completion == NULL) { @@ -113,29 +112,92 @@ static ServerAPI *_server = nil; if (error == nil) { NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; - completion(nil); - /* + + int status = [dataResult[@"status"] intValue]; + if (status == 1) { // status = 1 success + NSString *token = dataResult[@"result"][@"token"]; + NSDictionary *dictUser = dataResult[@"result"][@"user"]; + User *user = [[User alloc] init]; + user.user_id = [NSString stringWithFormat:@"%@",dictUser[@"id"]]; + user.username = [NSString stringWithFormat:@"%@",dictUser[@"username"]]; + user.full_name = [NSString stringWithFormat:@"%@",dictUser[@"full_name"]]; + user.nickname = [NSString stringWithFormat:@"%@",dictUser[@"nickname"]]; + user.email = [NSString stringWithFormat:@"%@",dictUser[@"email"]]; + user.password = [NSString stringWithFormat:@"%@",dictUser[@"password"]]; + user.birthday = [NSString stringWithFormat:@"%@",dictUser[@"birthday"]]; + user.address = [NSString stringWithFormat:@"%@",dictUser[@"address"]]; + user.gender = [[NSString stringWithFormat:@"%@",dictUser[@"gender"]] intValue]; + user.height = [[NSString stringWithFormat:@"%@",dictUser[@"height"]] floatValue]; + user.weight = [[NSString stringWithFormat:@"%@",dictUser[@"weight"]] floatValue]; + user.user_description = [NSString stringWithFormat:@"%@",dictUser[@"description"]]; + completion(user, token, nil); + } + else { // status = 0 error + NSString *message = dataResult[@"message"]; + NSError *loginFaild = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; + completion(nil, nil, loginFaild); + } + } + else + { + completion(nil, nil, error); + } + }]; +} + +- (void)forgetPass:(NSString *)email CompletionHandler:(void (^)(NSError *)) completion { + [self _request:[kServerAddress stringByAppendingFormat: @"forgetPass"] method:@"POST" paras:@{@"email":email} completion:^(NSData *data, NSError *error) { + + if (completion == NULL) { + return ; + } + + if (error == nil) + { NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; - if (dataResult == nil) { + + int status = [dataResult[@"status"] intValue]; + if (status == 1) { // status = 1 success completion(nil); } - + else { // status = 0 error + NSString *message = dataResult[@"message"]; + NSError *forgetPass = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; + completion(forgetPass); + } + } + else + { + completion(error); + } + }]; +} +- (void)confirmForgetPass:(NSString *)email withConfirm:(NSString *)confirm CompletionHandler:(void (^)(NSError *)) completion { + [self _request:[kServerAddress stringByAppendingFormat: @"forgetPass/confirm"] method:@"POST" paras:@{@"email":email, @"code_confirm": confirm} completion:^(NSData *data, NSError *error) { + + if (completion == NULL) { + return ; + } + + if (error == nil) + { + NSDictionary *dataResult = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingAllowFragments error: &error]; + int status = [dataResult[@"status"] intValue]; if (status == 1) { // status = 1 success completion(nil); } else { // status = 0 error NSString *message = dataResult[@"message"]; - NSError *registerFaild = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; - completion(registerFaild); + NSError *confirmForgetPass = [NSError errorWithDomain:@"LifeLog_Domain" code:-1 userInfo:@{@"message":message}]; + completion(confirmForgetPass); } - */ } else { completion(error); } - }]; + }]; } - (void)uploadImage:(NSString *)token andImageData:(NSData *)data CompletionHandler:(void (^)(NSString *, NSError *)) completion { diff --git a/LifeLog/LifeLog/User.m b/LifeLog/LifeLog/User.m index 503b2f2..92e7e0f 100644 --- a/LifeLog/LifeLog/User.m +++ b/LifeLog/LifeLog/User.m @@ -9,24 +9,42 @@ #import "User.h" @implementation User -//- (id)initWithCoder:(NSCoder *)decoder { -// self = [super init]; -// if (!self) { -// return nil; -// } -// -// self.user_id = [decoder decodeObjectForKey:@"user_id"]; -// self.access_token = [decoder decodeObjectForKey:@"access_token"]; -// self.full_name = [decoder decodeObjectForKey:@"full_name"]; -// self.picture = [decoder decodeObjectForKey:@"picture"]; -// -// return self; -//} -// -//- (void)encodeWithCoder:(NSCoder *)encoder { -// [encoder encodeObject:self.user_id forKey:@"user_id"]; -// [encoder encodeObject:self.access_token forKey:@"access_token"]; -// [encoder encodeObject:self.full_name forKey:@"full_name"]; -// [encoder encodeObject:self.picture forKey:@"picture"]; -//} +- (id)initWithCoder:(NSCoder *)decoder { + self = [super init]; + if (!self) { + return nil; + } + + self.user_id = [decoder decodeObjectForKey:@"user_id"]; + self.username = [decoder decodeObjectForKey:@"username"]; + self.password = [decoder decodeObjectForKey:@"password"]; + self.full_name = [decoder decodeObjectForKey:@"full_name"]; + self.nickname = [decoder decodeObjectForKey:@"nickname"]; + self.birthday = [decoder decodeObjectForKey:@"birthday"]; + self.height = [decoder decodeFloatForKey:@"height"]; + self.weight = [decoder decodeFloatForKey:@"weight"]; + self.gender = [decoder decodeIntForKey:@"gender"]; + self.address = [decoder decodeObjectForKey:@"address"]; + self.user_description = [decoder decodeObjectForKey:@"user_description"]; + self.email = [decoder decodeObjectForKey:@"email"]; + self.phone = [decoder decodeObjectForKey:@"phone"]; + + return self; +} + +- (void)encodeWithCoder:(NSCoder *)encoder { + [encoder encodeObject:self.user_id forKey:@"user_id"]; + [encoder encodeObject:self.username forKey:@"username"]; + [encoder encodeObject:self.password forKey:@"password"]; + [encoder encodeObject:self.full_name forKey:@"full_name"]; + [encoder encodeObject:self.nickname forKey:@"nickname"]; + [encoder encodeObject:self.birthday forKey:@"birthday"]; + [encoder encodeFloat:self.height forKey:@"height"]; + [encoder encodeFloat:self.weight forKey:@"weight"]; + [encoder encodeInt:self.gender forKey:@"gender"]; + [encoder encodeObject:self.address forKey:@"address"]; + [encoder encodeObject:self.user_description forKey:@"user_description"]; + [encoder encodeObject:self.email forKey:@"email"]; + [encoder encodeObject:self.phone forKey:@"phone"]; +} @end diff --git a/LifeLog/LifeLog/Utilities.h b/LifeLog/LifeLog/Utilities.h index d13f456..c74517e 100644 --- a/LifeLog/LifeLog/Utilities.h +++ b/LifeLog/LifeLog/Utilities.h @@ -12,4 +12,5 @@ @interface Utilities : NSObject + (NSString *)addCommaFromNumber:(NSInteger)number; + (UIColor *)convertHecToColor:(int) hex; ++ (void)showErrorMessage:(NSString *)message withViewController:(UIViewController *)vc; @end diff --git a/LifeLog/LifeLog/Utilities.m b/LifeLog/LifeLog/Utilities.m index 01994a4..8e6fdb7 100644 --- a/LifeLog/LifeLog/Utilities.m +++ b/LifeLog/LifeLog/Utilities.m @@ -25,4 +25,27 @@ blue:((float)(hex & 0xFF))/255.0 alpha:1.0]; } ++ (void)showErrorMessage:(NSString *)message withViewController:(UIViewController *)vc +{ + if (message.length > 0) { + UIAlertController * alert= [UIAlertController + alertControllerWithTitle:@"Error" + message:message + preferredStyle:UIAlertControllerStyleAlert]; + + UIAlertAction* ok = [UIAlertAction + actionWithTitle:@"OK" + style:UIAlertActionStyleDefault + handler:^(UIAlertAction * action) + { + [alert dismissViewControllerAnimated:YES completion:nil]; + + }]; + + [alert addAction:ok]; + + [vc presentViewController:alert animated:YES completion:nil]; + } +} + @end -- 1.8.5.3