IOS 使用shareSDK發送驗證碼

//
//  LoginMobileController.m
//  kykx
//  手機發送驗證碼
//  Created by zhangyg on 15/7/9.
//  Copyright (c) 2015年 ytint. All rights reserved.
//

#import "LoginMobileController.h"
#import <SMS_SDK/SMS_SDK.h>
#import <SMS_SDK/CountryAndAreaCode.h>
#import "CommenData.h"
#import "DocPackage.h"
#import "AFHTTPRequestOperation.h"
@interface LoginMobileController ()
{
    NSTimer* _timer1;
    NSTimer* _timer2;
    NSTimer* _timer3;
}

@end

static int count = 0;

@implementation LoginMobileController

-(void)viewDidLoad{
    [super viewDidLoad];
    //將返回按鈕的文字設置不在界面顯示
    [[UIBarButtonItem appearance]setBackButtonTitlePositionAdjustment:UIOffsetMake(NSIntegerMin, NSIntegerMin) forBarMetrics:UIBarMetricsDefault];

    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    self.mobile.delegate=self;
    self.icode.delegate=self;
    NSLog(@"%@",self.showWho);
    if([self.showWho isEqualToString:@"1"]){
        //修改密碼
    }else{
        //註冊
        self.navigationItem.title=@"註冊";
    }

    //發送驗證碼
    UITapGestureRecognizer *sendcodeGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(sendCodeAction)];
    self.sendCode.userInteractionEnabled=YES;
    [self.sendCode addGestureRecognizer:sendcodeGesture];

    //下一步
    UITapGestureRecognizer *nextGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(nextAction)];
    self.nextPass.userInteractionEnabled=YES;
    [self.nextPass addGestureRecognizer:nextGesture];

}
-(NSInteger)mobileIsRepeat{
    //註冊手機號是否重複
    NSString *stringurl = [NSString stringWithFormat:@"%@cdpt/api/user_is_exist?access_token=0&user_name=%@", BaseURLString,self.mobile.text];
    NSURL *url = [NSURL URLWithString:stringurl];
    NSURLRequest *request=[[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:2];
    NSError *error;
    NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
    if (error) {
        NSLog(@"error=%@",error);
        [CommenData showNetError:self.navigationController.view];
    }else{
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",dict);
        NSNumber *code=[dict code];
        if ([code intValue]==200) {
            if ([[dict infoString] intValue]==0) {
                //手機號碼沒有註冊
                return 1;
            }else{
                //手機號碼已經註冊
                return 0;
            }
        }else{
            [CommenData showMsgError:self.navigationController.view showMsg:[dict msg]];
        }
    }
    return -1;

}

-(void)sendCodeAction{

    NSString *regex = @"1[0-9]{10}";

    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];

    BOOL isMatch = [pred evaluateWithObject:self.mobile.text];
    if (!isMatch){
        //手機號碼不正確
        [CommenData showMsgError:self.navigationController.view showMsg:@"手機號碼不正確"];

        return;

    }
    if([self.showWho isEqualToString:@"1"]){
        //修改密碼
        if ([self mobileIsRepeat]!=0) {
            if([self mobileIsRepeat]==1){
                [CommenData showMsgError:self.navigationController.view showMsg:@"該手機號碼沒有註冊"];
            }
            return;
        }
    }else{
        //註冊
        if ([self mobileIsRepeat]!=1) {
            if([self mobileIsRepeat]==0){
                [CommenData showMsgError:self.navigationController.view showMsg:@"該手機號碼已經註冊"];
            }
            return;
        }
    }

    [SMS_SDK getVerificationCodeBySMSWithPhone:self.mobile.text
                                          zone:@"86"
                                        result:^(SMS_SDKError *error)
     {
         if (!error)
         {
             //更新時間
             [_timer2 invalidate];

             count = 0;

             NSTimer* timer2=[NSTimer scheduledTimerWithTimeInterval:1
                                                              target:self
                                                            selector:@selector(updateTime)
                                                            userInfo:nil
                                                             repeats:YES];
             _timer2=timer2;

         }
         else
         {
             [CommenData showMsgError:self.navigationController.view showMsg:error.errorDescription];
         }

     }];

}
-(void)updateTime
{
    count++;
    if (count>=60)
    {
        [_timer2 invalidate];

        self.sendCode.text=@"重新獲取驗證碼";
        self.sendCode.userInteractionEnabled=YES;

        return;
    }
    //NSLog(@"更新時間");
    self.sendCode.text=[NSString stringWithFormat:@"%i秒後可重新發送",60-count];
    self.sendCode.userInteractionEnabled=NO;

}

-(void)nextAction{

    //驗證號碼

    if(self.icode.text.length!=4)
    {
        [CommenData showMsgError:self.navigationController.view showMsg:@"驗證碼錯誤"];
    }
    else
    {
        [SMS_SDK commitVerifyCode:self.icode.text result:^(enum SMS_ResponseState state) {
            if (1==state)
            {
                NSLog(@"驗證成功");
                //下一步 修改密碼
                UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
                self.loginPasswordController=[story instantiateViewControllerWithIdentifier:@"loginPasswordPage"];

                if([self.showWho isEqualToString:@"1"]){
                    //修改密碼
                    self.loginPasswordController.showWho=@"1";
                }else{
                    //註冊
                    self.loginPasswordController.showWho=@"2";
                }
                self.loginPasswordController.mobile=self.mobile.text;
                [self.navigationController pushViewController:self.loginPasswordController animated:YES];
            }
            else if(0==state)
            {
                NSLog(@"驗證失敗");
                [CommenData showMsgError:self.navigationController.view showMsg:@"驗證碼失敗"];
            }
        }];
    }



}

//點擊空白處隱藏鍵盤
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.mobile resignFirstResponder];
    [self.icode resignFirstResponder];
}
//改變輸入框背景圖片
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField.tag==5) {
        [self.mobile setBackground:[UIImage imageNamed:@"inputlan2.png"]];
        [self.icode setBackground:[UIImage imageNamed:@"inputhui2.png"]];
    }else if(textField.tag==6){
        [self.icode setBackground:[UIImage imageNamed:@"inputlan2.png"]];
        [self.mobile setBackground:[UIImage imageNamed:@"inputhui2.png"]];
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.tag==5) {
        [self.mobile setBackground:[UIImage imageNamed:@"inputhui2.png"]];
    }else if(textField.tag==6){
        [self.icode setBackground:[UIImage imageNamed:@"inputhui2.png"]];
    }
}

@end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章