Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // LoginViewController.m
- // IronWorldProject
- //
- // Created by Studio Vision on 23.11.15.
- // Copyright © 2015 Studio Vision. All rights reserved.
- //
- #import "LoginViewController.h"
- #import "AVFoundation/AVFoundation.h"
- #import "EnterModeViewController.h"
- #import "MainMenuViewController.h"
- #import "ForgotPasswordViewController.h"
- #import <CommonCrypto/CommonDigest.h>
- #import "ServerManager.h"
- #import <Foundation/Foundation.h>
- #import <VKSdk/VKSdk.h>
- #import <FBSDKCoreKit/FBSDKCoreKit.h>
- #import <FBSDKLoginKit/FBSDKLoginKit.h>
- #import <FBSDKShareKit/FBSDKShareKit.h>
- #import <QuartzCore/QuartzCore.h>
- #import <AdSupport/AdSupport.h>
- #import <CoreMotion/CoreMotion.h>
- #import "GMDCircleLoader.h"
- @interface LoginViewController () <UITextFieldDelegate,
- VKSdkUIDelegate, VKSdkDelegate>
- @property (weak, nonatomic) IBOutlet UIView *titleContainerView;
- @property (weak, nonatomic) IBOutlet UITextField *loginTextField;
- @property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *heightLayout;
- @property (weak, nonatomic) IBOutlet NSLayoutConstraint *widthLayout;
- @property (weak, nonatomic) IBOutlet UIView *containerView;
- @property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;
- @property (weak, nonatomic) IBOutlet UIButton *enterButton;
- @property (weak, nonatomic) IBOutlet UIButton *forgetPasswordButton;
- @property (weak, nonatomic) IBOutlet UILabel *titleLabel;
- @property (weak, nonatomic) UIView * backgroundBlackView;
- @property (weak, nonatomic) UIView * loadBlackView;
- @end
- static NSString * const TOKEN_KEY = @"CNYwWzVi7a9BDaKGQOf8";
- static NSArray * SCOPE = nil;
- static NSInteger loadBlackViewTag = 99;
- static NSInteger iphoneLimit = 414;
- static NSInteger iphone5Height = 568;
- @implementation LoginViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- NSInteger textFieldFontSize = 14;
- NSInteger titleLabelFontSize = 18;
- NSInteger buttonFontSize = 15;
- NSInteger passwordButton = 16;
- if (self.view.frame.size.width > iphoneLimit) {
- textFieldFontSize = 17;
- titleLabelFontSize = 23;
- buttonFontSize = 20;
- passwordButton = 19;
- }
- [self.loginTextField setFont:[UIFont fontWithName:@"Roboto-Medium"
- size:textFieldFontSize]];
- [self.titleLabel setFont:[UIFont fontWithName:@"Roboto-Medium"
- size:titleLabelFontSize]];
- [self.enterButton.titleLabel setFont:[UIFont fontWithName:@"Roboto-Medium"
- size:buttonFontSize]];
- [self.forgetPasswordButton.titleLabel setFont:[UIFont fontWithName:@"Roboto-Medium"
- size:passwordButton]];
- [self.loginTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
- [self.passwordTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
- self.loginTextField.layer.borderColor = [UIColor redColor].CGColor;
- self.loginTextField.layer.borderWidth = 0;
- self.passwordTextField.layer.borderColor = [UIColor redColor].CGColor;
- self.passwordTextField.layer.borderWidth = 0;
- self.loginTextField.delegate = self;
- self.passwordTextField.delegate = self;
- // scroll
- //self.myScrollView.pagingEnabled = YES;
- //self.automaticallyAdjustsScrollViewInsets = NO;
- self.heightLayout.constant = self.view.frame.size.height - self.titleContainerView.frame.size.height;
- self.widthLayout.constant = self.view.frame.size.width;
- UIView * loadBlackView = [[UIView alloc]initWithFrame:self.view.frame];
- loadBlackView.backgroundColor = [UIColor blackColor];
- loadBlackView.alpha = 0;
- loadBlackView.tag = loadBlackViewTag;
- [self.view addSubview:loadBlackView];
- self.loadBlackView = loadBlackView;
- // VK
- SCOPE = @[VK_PER_FRIENDS, VK_PER_WALL, VK_PER_AUDIO, VK_PER_PHOTOS, VK_PER_NOHTTPS, VK_PER_EMAIL, VK_PER_MESSAGES];
- [[VKSdk initializeWithAppId:@"5184196"] registerDelegate:self];
- [[VKSdk instance] setUiDelegate:self];
- for (UIView * subView in self.view.subviews)
- subView.alpha = 0;
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
- [UIView animateWithDuration:0.7f animations:^{
- for (UIView * subView in self.view.subviews) {
- if (subView.tag != loadBlackViewTag)
- subView.alpha = 1;
- }
- } completion:^(BOOL finished) {
- //self.loadBlackView.alpha = 0.6;
- //[GMDCircleLoader setOnView:self.view withTitle:@"Loading " animated:YES];
- }];
- }
- #pragma mark -Message
- - (void) showMessageWithTitle:(NSString *) message {
- UIView * backgroundBlackView = [[UIView alloc]initWithFrame:self.view.frame];
- backgroundBlackView.backgroundColor = [UIColor blackColor];
- backgroundBlackView.alpha = 0;
- [self.view addSubview:backgroundBlackView];
- self.backgroundBlackView = backgroundBlackView;
- UITapGestureRecognizer *singleFingerTap =
- [[UITapGestureRecognizer alloc] initWithTarget:self
- action:@selector(hideMessage:)];
- [backgroundBlackView addGestureRecognizer:singleFingerTap];
- CGFloat labelHeight = [self getTextHeight:message];
- UILabel * messageLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, labelHeight)];
- messageLabel.text = message;
- messageLabel.font = [UIFont fontWithName:@"Roboto-Medium" size:15];
- messageLabel.textAlignment = UIBaselineAdjustmentAlignCenters;
- messageLabel.numberOfLines = 0;
- messageLabel.textColor = [UIColor whiteColor];
- messageLabel.center = backgroundBlackView.center;
- [backgroundBlackView addSubview:messageLabel];
- UIImageView * messageIconImage = [[UIImageView alloc]initWithFrame:CGRectMake(1, 1, 50, 50)];
- messageIconImage.image = [UIImage imageNamed:@"userAvatar.png"];
- messageIconImage.center = CGPointMake(backgroundBlackView.frame.size.width / 2, messageLabel.frame.origin.y - 25);
- [self.backgroundBlackView addSubview:messageIconImage];
- UILabel * okLabel = [[UILabel alloc]initWithFrame:CGRectMake(-1, messageLabel.frame.origin.y + labelHeight - 15, self.view.frame.size.width + 2, 60)];
- okLabel.textColor = [UIColor whiteColor];
- okLabel.textAlignment = UIBaselineAdjustmentAlignCenters;
- okLabel.text = @"Ok";
- okLabel.font = [UIFont fontWithName:@"Roboto-Medium" size:20];
- okLabel.layer.borderColor = [UIColor whiteColor].CGColor;
- okLabel.layer.borderWidth = 1;
- [backgroundBlackView addSubview:okLabel];
- [UIView animateWithDuration:0.4 animations:^{
- backgroundBlackView.alpha = 0.8;
- } completion:^(BOOL finished) {
- }];
- }
- - (CGFloat) getTextHeight:(NSString*) text {
- CGFloat offset = 5.0;
- //UIFont * font = [UIFont systemFontOfSize:15.f];
- UIFont * font = [UIFont fontWithName:@"Roboto-Medium" size:15];
- NSShadow * shadow = [[NSShadow alloc] init];
- shadow.shadowOffset = CGSizeMake(0, -1);
- shadow.shadowBlurRadius = 0.5;
- NSMutableParagraphStyle * paragraph = [[NSMutableParagraphStyle alloc] init];
- [paragraph setLineBreakMode:NSLineBreakByWordWrapping];
- [paragraph setAlignment:NSTextAlignmentCenter];
- NSDictionary * attributes =
- [NSDictionary dictionaryWithObjectsAndKeys:
- font, NSFontAttributeName,
- paragraph, NSParagraphStyleAttributeName,
- shadow, NSShadowAttributeName, nil];
- CGRect rect = [text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 2 * offset, CGFLOAT_MAX)
- options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
- attributes:attributes
- context:nil];
- return CGRectGetHeight(rect) + 2 * offset;
- }
- - (void)hideMessage:(UITapGestureRecognizer *)recognizer {
- [UIView animateWithDuration:0.2 animations:^{
- self.backgroundBlackView.alpha = 0;
- } completion:^(BOOL finished) {
- }];
- }
- - (void)fieldValidation {
- if ([self.loginTextField.text isEqualToString:@""])
- self.loginTextField.layer.borderWidth = 1;
- else
- self.loginTextField.layer.borderWidth = 0;
- if ([self.passwordTextField.text isEqualToString:@""])
- self.passwordTextField.layer.borderWidth = 1;
- else
- self.passwordTextField.layer.borderWidth = 0;
- }
- #pragma mark -UITextFieldDelegate
- - (BOOL)textFieldShouldReturn:(UITextField *)textField {
- if (textField == self.loginTextField) {
- [self.passwordTextField becomeFirstResponder];
- } else {
- [self actionEnter:nil];
- }
- return YES;
- }
- - (void)textFieldDidBeginEditing:(UITextField *)textField {
- textField.layer.borderWidth = 0;
- self.myScrollView.scrollEnabled = YES;
- if (self.view.frame.size.height <= iphone5Height) {
- self.heightLayout.constant = 580;
- NSInteger scrollPos = 80;
- if (self.view.frame.size.height == 480) {
- scrollPos = 150;
- }
- [self.myScrollView setContentOffset:CGPointMake(self.myScrollView.frame.origin.x, scrollPos)
- animated:YES];
- }
- }
- - (void)textFieldDidEndEditing:(UITextField *)textField {
- if (self.view.frame.size.height <= iphone5Height) {
- [self.myScrollView setContentOffset:CGPointMake(self.myScrollView.frame.origin.x, 0)
- animated:YES];
- self.myScrollView.scrollEnabled = NO;
- }
- }
- #pragma mark -VKSdkUIDelegate
- - (void)vkSdkShouldPresentViewController:(UIViewController *)controller {
- [self.navigationController.topViewController presentViewController:controller animated:YES completion:nil];
- }
- - (void)vkSdkNeedCaptchaEnter:(VKError *)captchaError {
- VKCaptchaViewController *vc = [VKCaptchaViewController captchaControllerWithError:captchaError];
- [vc presentIn:self.navigationController.topViewController];
- //[self presentViewController:vc animated:YES completion:nil];
- }
- #pragma mark -VKSdkDelegate
- - (void)vkSdkTokenHasExpired:(VKAccessToken *)expiredToken {
- [self actionLogVK:nil];
- }
- - (void)vkSdkUserAuthorizationFailed {
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- - (void)vkSdkAccessAuthorizationFinishedWithResult:(VKAuthorizationResult *)result {
- if (result.token) {
- [self.view endEditing:YES];
- [self dismissViewControllerAnimated:YES completion:nil];
- NSLog(@"_________________________________________");
- NSLog(@"Token: %@",result.token.accessToken);
- NSLog(@"id: %@",result.user.id);
- NSLog(@"first_name: %@",result.user.first_name);
- NSLog(@"last_name: %@",result.user.last_name);
- NSLog(@"sex: %@",result.user.sex);
- NSLog(@"bdate: %@",result.user.bdate);
- NSLog(@"photo_100: %@",result.user.photo_100);
- NSLog(@"email: %@",result.token.email);
- if (result.user.first_name && result.user.last_name &&
- result.user.sex && result.user.bdate && result.user.photo_100) {
- NSString * sexStr = @"1";
- if (result.user.sex == 0)
- sexStr = @"0";
- [self saveDataAboutUserWithFirstName:result.user.first_name
- lastName:result.user.last_name
- userEmail:@"vkEmail"
- userGender:sexStr
- userPassword:@"vkPassword"
- userBirthdaty:result.user.bdate
- userAvatar:result.user.photo_100];
- } else {
- [self showMessageWithTitle:@"Нету всех данных\n"];
- }
- } else if (result.error) {
- NSLog(@"%@",result.error);
- }
- }
- #pragma mark -Server
- - (void)loginUserWithEmail:(NSString *)email andPassword:(NSString *)password {
- NSString * md5Password = [self generateMD5:password];
- self.loadBlackView.alpha = 0.6;
- [GMDCircleLoader setOnView:self.view withTitle:@"Loading..." animated:YES];
- [[ServerManager sharedManager]loginUserWithEmail:self.loginTextField.text password:md5Password onSuccess:^(id resultJSON) {
- NSString * requestStatus = resultJSON[@"status"];
- if ([requestStatus intValue] == SUCCESS) {
- for (UIView * subView in self.view.subviews)
- [subView removeFromSuperview];
- MainMenuViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"MainMenuViewController"];
- [self addChildViewController:vc];
- [self.view addSubview:vc.view];
- [vc didMoveToParentViewController:self];
- vc.view.frame = CGRectMake(0,
- self.view.frame.size.height - vc.view.frame.size.height,
- vc.view.frame.size.width,
- vc.view.frame.size.height);
- } else if ([requestStatus intValue] == EMAIL_NOT_FOUND) {
- [self showMessageWithTitle:@"Can't find this email\n"];
- } else if ([requestStatus intValue] == WRONG_PASSWORD) {
- [self showMessageWithTitle:@"Wrong password\n"];
- } else {
- [self showMessageWithTitle:@"Opp's something going wrong\n"];
- }
- self.loadBlackView.alpha = 0;
- [GMDCircleLoader hideFromView:self.view animated:YES];
- } onFailure:^(NSError *error) {
- if (error.code == CONNECTION_ERROR) {
- [self showMessageWithTitle:@"You are offline\n"];
- } else
- [self showMessageWithTitle:@"Opp's something going wrong\n"];
- self.loadBlackView.alpha = 0;
- [GMDCircleLoader hideFromView:self.view animated:YES];
- }];
- }
- - (NSString *)generateMD5:(NSString *)input {
- const char * cStr = [input UTF8String];
- unsigned char digest[CC_MD5_DIGEST_LENGTH];
- CC_MD5( cStr, (CC_LONG)strlen(cStr), digest);
- NSMutableString * output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
- for(NSInteger i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
- [output appendFormat:@"%02x", digest[i]];
- return output;
- }
- #pragma mark -Action
- - (IBAction)actionBack:(id)sender {
- [self.view endEditing:YES];
- for (UIView * subView in self.view.subviews)
- [subView removeFromSuperview];
- EnterModeViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"EnterModeViewController"];
- [self addChildViewController:vc];
- [self.view addSubview:vc.view];
- [vc didMoveToParentViewController:self];
- vc.view.frame = CGRectMake(0,
- self.view.frame.size.height - vc.view.frame.size.height,
- vc.view.frame.size.width,
- vc.view.frame.size.height);
- }
- - (IBAction)actionEnter:(id)sender {
- [self.view endEditing:YES];
- [self fieldValidation];
- if ([self.loginTextField.text isEqualToString:@""] || [self.passwordTextField.text isEqualToString:@""]) {
- [self showMessageWithTitle:@"enter all fields\n"];
- } else {
- [self loginUserWithEmail:self.loginTextField.text andPassword:self.passwordTextField.text];
- }
- }
- - (IBAction)actionForgotPassword:(id)sender {
- [self.view endEditing:YES];
- for (UIView * subView in self.view.subviews)
- [subView removeFromSuperview];
- ForgotPasswordViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ForgotPasswordViewController"];
- [self addChildViewController:vc];
- [self.view addSubview:vc.view];
- [vc didMoveToParentViewController:self];
- vc.view.frame = CGRectMake(0,
- self.view.frame.size.height - vc.view.frame.size.height,
- vc.view.frame.size.width,
- vc.view.frame.size.height);
- }
- #pragma mark -SocialAction
- - (IBAction)actionLogVK:(id)sender {
- [VKSdk forceLogout];
- //[VKSdk authorize:SCOPE];
- VKShareDialogController *shareDialog = [VKShareDialogController new];
- [shareDialog setCompletionHandler:^(VKShareDialogController *dialog, VKShareDialogControllerResult result) {
- [self dismissViewControllerAnimated:YES completion:nil];
- }];
- [self presentViewController:shareDialog animated:YES completion:nil];
- }
- - (IBAction)actionLogFB:(id)sender {
- //[FBSDKAccessToken setCurrentAccessToken:nil];
- FBSDKLoginManager * login = [[FBSDKLoginManager alloc] init];
- if ([FBSDKAccessToken currentAccessToken]) {
- [self getUserInfoFromFB];
- } else
- {
- [login logInWithReadPermissions:@[@"email", @"user_birthday"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
- {
- if (error) {
- NSLog(@"Login error: %@",error);
- } else {
- NSLog(@"Login Success");
- [self getUserInfoFromFB];
- }
- }];
- }
- }
- - (void)getUserInfoFromFB
- {
- if ([FBSDKAccessToken currentAccessToken]) {
- NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
- NSDictionary * params = @{@"fields": @"id, name, email, last_name, first_name, birthday,gender, picture"};
- self.loadBlackView.alpha = 0.6;
- [GMDCircleLoader setOnView:self.view withTitle:@"Loading..." animated:YES];
- [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:params] startWithCompletionHandler:
- ^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
- if (error) {
- [self showMessageWithTitle:@"Error, while get fb info\n"];
- self.loadBlackView.alpha = 0;
- [GMDCircleLoader hideFromView:self.view animated:YES];
- NSLog(@"FB user info error: %@",error);
- } else {
- NSLog(@"FB user info: %@",result);
- NSDictionary * userInfoDict = result;
- NSDictionary * pictureData = userInfoDict[@"picture"];
- NSDictionary * dataFromPictureData = pictureData[@"data"];
- NSString * fbAvatarURLStr = dataFromPictureData[@"url"];
- NSString * genderStr = @"1";
- if ([userInfoDict[@"gender"] isEqualToString:@"female"])
- genderStr = @"0";
- [[ServerManager sharedManager]authoSocialNetworkWithFirstName:userInfoDict[@"first_name"]
- lastName:userInfoDict[@"last_name"]
- email:userInfoDict[@"email"]
- gender:genderStr
- birthday:userInfoDict[@"birthday"]
- typeOfSN:@"Facebook"
- userID:userInfoDict[@"id"]
- avatarURL:fbAvatarURLStr
- onSuccess:^(id resultJSON) {
- self.loadBlackView.alpha = 0;
- [GMDCircleLoader hideFromView:self.view animated:YES];
- } onFailure:^(NSError *error) {
- self.loadBlackView.alpha = 0;
- [GMDCircleLoader hideFromView:self.view animated:YES];
- }];
- // [self saveDataAboutUserWithFirstName:userInfoDict[@"first_name"]
- // lastName:userInfoDict[@"last_name"]
- // userEmail:userInfoDict[@"email"]
- // userGender:genderStr
- // userPassword:@"fbPassword"
- // userBirthdaty:userInfoDict[@"birthday"]
- // userAvatar:fbAvatarURLStr];
- }
- }];
- }
- }
- - (void)saveDataAboutUserWithFirstName:(NSString *)firstName
- lastName:(NSString *)lastName
- userEmail:(NSString *)email
- userGender:(NSString *)gender
- userPassword:(NSString *) password
- userBirthdaty:(NSString *)birthday
- userAvatar:(NSString *)avatar {
- NSUserDefaults * userDef = [NSUserDefaults standardUserDefaults];
- [userDef setObject:firstName forKey:@"firstNameKey"];
- [userDef setObject:lastName forKey:@"lastNameKey"];
- [userDef setObject:email forKey:@"emailKey"];
- [userDef setObject:gender forKey:@"genderKey"];
- [userDef setObject:password forKey:@"passwordKey"];
- [userDef setObject:birthday forKey:@"birthdayKey"];
- [userDef setObject:avatar forKey:@"avatarKey"];
- [userDef synchronize];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement