Logic.h
1 2 3 4 5 6 7 8 9 10 11 12 13 |
typedef BOOL (^OnUserPwdChanged) (BOOL changed, NSString * pwd); ... @interface Logic : NSObject { } //edit user password -(BOOL)editUserPwdEmail:(NSString*)email replaceOldPassword:(NSString*)oldPwd withNewPassword:(NSString*)newPwd onSuccess:(OnUserPwdChanged)userPwdChangedBlk; |
Logic.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 |
-(BOOL)editUserPwdEmail:(NSString*)email replaceOldPassword:(NSString*)oldPwd withNewPassword:(NSString*)newPwd onSuccess:(OnUserPwdChanged)userPwdChangedBlk { UserDB * userDB = [[UserDB alloc] initAndCreateUserTable]; //checks to see if user table has been created //get old password of user with APPDELEGATE.currentUser.email NSString * oldPasswordByEmail = [userDB getPasswordByEmail:email]; NSString * userEnteredOldPwd = oldPwd; //then check to see if that old password matches our [oldPassword text] if([oldPasswordByEmail compare:userEnteredOldPwd]==NSOrderedSame) { //if it does, then we update that user's email with our new password [userDB updateUserPasswordByEmail:email withNewPassword:newPwd]; return userPwdChangedBlk(TRUE, newPwd); } else { return userPwdChangedBlk(FALSE, newPwd); } } |
…then when you access the method call with your own block definition like so:
1 2 3 4 5 6 7 8 9 10 11 |
-(BOOL)processSave { ... ... Logic * appLogic = [[Logic alloc] init]; return [appLogic editUserPwdEmail:APPDELEGATE.currentUser.email replaceOldPassword:[oldPassword text] withNewPassword:[newPasswordConfirm text] onSuccess:^BOOL(BOOL changed, NSString *pwd) { ...blah blah return (changed) ? TRUE : FALSE; }]; } |
How it runs through…
First, the code runs until it hits return userPwdChangedBlk(TRUE, newPwd);
1 2 3 4 5 6 7 |
//then check to see if that old password matches our [oldPassword text] if([oldPasswordByEmail compare:userEnteredOldPwd]==NSOrderedSame) { //if it does, then we update that user's email with our new password [userDB updateUserPasswordByEmail:email withNewPassword:newPwd]; return userPwdChangedBlk(TRUE, newPwd); } |
We get the new password, then pass in TRUE, and the new password so the block of the caller can run.
So in the outside processSave method, the TRUE and new password gets passed into the parameter, and you evaluate it. Then return TRUE or FALSE.
1 2 3 4 5 6 |
onSuccess:^BOOL(BOOL changed, NSString *pwd) { ...blah blah return (changed) ? TRUE : FALSE; }]; } |
This return from the onSuccess block will be the result of
1 |
return userPwdChangedBlk(TRUE, newPwd); |
in Logic.m’s
1 2 3 4 5 6 |
if([oldPasswordByEmail compare:userEnteredOldPwd]==NSOrderedSame) { //if it does, then we update that user's email with our new password [userDB updateUserPasswordByEmail:email withNewPassword:newPwd]; return userPwdChangedBlk(TRUE, newPwd); } |