Swift免費(fèi)短信驗(yàn)證碼實(shí)現(xiàn)及動(dòng)態(tài)倒計(jì)時(shí)功能
今天給大家?guī)?lái)一個(gè)簡(jiǎn)單的免費(fèi)短信驗(yàn)證碼實(shí)現(xiàn)demo,采用mob的短信驗(yàn)證碼SDK,到目前為止還是免費(fèi)的,只需要簡(jiǎn)單的注冊(cè)--》添加個(gè)人應(yīng)用--》獲取appkey集apSecret 即可實(shí)現(xiàn)。
具體怎么申請(qǐng),添加個(gè)人應(yīng)用這里就不累贅了,相信能搜索到本文的必然有能力完成上面的操作。
1、下載mob的免費(fèi)短信驗(yàn)證SDK,解壓后復(fù)制SMS_SDK到你的工程,因?yàn)榇薙DK采用OC編寫(xiě)的,在與Swift結(jié)合時(shí),需要添加橋接文件,具體操作如下:
右鍵你的Swift工程,新建一個(gè)OC文件,名字隨便起,這時(shí)會(huì)彈出提示你創(chuàng)建一個(gè)橋接文件,點(diǎn)擊是就OK了!在你的工程中會(huì)多出一個(gè)以工程名--Bridging-Header.h的文件,打開(kāi)寫(xiě)入下面的代碼:
#import <SMS_SDK/SMSSDK.h>
當(dāng)然,創(chuàng)建橋接文件的方法有很多種,會(huì)的就無(wú)需關(guān)注咯。
2、打開(kāi)工程中的storyboard,創(chuàng)建一個(gè)電話號(hào)碼文本框、驗(yàn)證碼文本框、獲取驗(yàn)證碼按鈕、提交驗(yàn)證按鈕。并對(duì)相關(guān)操作進(jìn)行ViewController連線,如下圖:
3、在AppDelegate.swift文件中的func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool中添加如下代碼:
SMSSDK.registerApp(你的appKey withSecret: 你的appSecret)
4、編寫(xiě)ViewController.swift,具體就看代碼吧,很簡(jiǎn)單的一個(gè)小功能,請(qǐng)各位自行擴(kuò)展吧。
// // ViewController.swift // Yundou // // Created by Slow on 16/1/2. // Copyright (c) 2016年 Ivan. All rights reserved. // import UIKit class ViewController: UIViewController { @IBOutlet weak var getAuthCodeButton: UIButton! //驗(yàn)證碼文本框 @IBOutlet weak var authCodeText: UITextField! //手機(jī)號(hào)碼文本框 @IBOutlet weak var phoneText: UITextField! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //獲取驗(yàn)證碼 @IBAction func getAuthCode(sender: UIButton) { var phoneNum = phoneText.text SMSSDK.getVerificationCodeByMethod(SMSGetCodeMethodSMS, phoneNumber:phoneNum, zone: "86",customIdentifier: nil,result: {(error: NSError!) ->Void in if(error == nil){ NSLog("發(fā)送成功") self.countDown(60) }else{ NSLog("發(fā)送失??!%@" , error) } }) } //提交驗(yàn)證碼 @IBAction func submitAuthCode(sender: UIButton) { var authCode = authCodeText.text var phoneNum = phoneText.text var resultMessage = "" SMSSDK.commitVerificationCode(authCode, phoneNumber: phoneNum, zone: "86" , result:{ (error: NSError!) -> Void in if(error == nil){ resultMessage = "恭喜您,驗(yàn)證成功!" NSLog("驗(yàn)證成功") }else{ resultMessage = "很抱歉,驗(yàn)證失??!" NSLog("驗(yàn)證失??!" , error) } let resultAlertView:UIAlertView = UIAlertView(title: "驗(yàn)證結(jié)果", message: resultMessage, delegate: nil, cancelButtonTitle: "確定") resultAlertView.show() }) } //驗(yàn)證碼倒計(jì)時(shí) func countDown(timeOut:Int){ //倒計(jì)時(shí)時(shí)間 var timeout = timeOut var queue:dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); var _timer:dispatch_source_t = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue) dispatch_source_set_timer(_timer, dispatch_walltime(nil, 0), 1*NSEC_PER_SEC, 0) //每秒執(zhí)行 dispatch_source_set_event_handler(_timer, { () -> Void in if(timeout<=0){ //倒計(jì)時(shí)結(jié)束,關(guān)閉 dispatch_source_cancel(_timer); dispatch_sync(dispatch_get_main_queue(), { () -> Void in //設(shè)置界面的按鈕顯示 根據(jù)自己需求設(shè)置 self.getAuthCodeButton.setTitle("再次獲取", forState: UIControlState.Normal) }) }else{//正在倒計(jì)時(shí) var seconds = timeout % 60 var strTime = NSString.localizedStringWithFormat("%.2d", seconds) dispatch_sync(dispatch_get_main_queue(), { () -> Void in // NSLog("----%@", NSString.localizedStringWithFormat("%@S", strTime) as String) UIView.beginAnimations(nil, context: nil) UIView.setAnimationDuration(1) //設(shè)置界面的按鈕顯示 根據(jù)自己需求設(shè)置 self.getAuthCodeButton.setTitle(NSString.localizedStringWithFormat("%@S", strTime) as String, forState: UIControlState.Normal) UIView.commitAnimations() self.getAuthCodeButton.userInteractionEnabled = false }) timeout--; } }) dispatch_resume(_timer) } }
以上所述是小編給大家介紹的Swift免費(fèi)短信驗(yàn)證碼實(shí)現(xiàn)及動(dòng)態(tài)倒計(jì)時(shí)功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring中BeanFactory與FactoryBean的區(qū)別解讀
這篇文章主要介紹了Spring中BeanFactory與FactoryBean的區(qū)別解讀,Java的BeanFactory是Spring框架中的一個(gè)接口,它是用來(lái)管理和創(chuàng)建對(duì)象的工廠接口,在Spring中,我們可以定義多個(gè)BeanFactory來(lái)管理不同的組件,需要的朋友可以參考下2023-12-12LeetCode?刷題?Swift?兩個(gè)數(shù)組的交集
這篇文章主要為大家介紹了LeetCode?刷題?Swift?兩個(gè)數(shù)組的交集示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09深入解析Swift中switch語(yǔ)句對(duì)case的數(shù)據(jù)類型匹配的支持
這篇文章主要介紹了Swift中switch語(yǔ)句對(duì)case的數(shù)據(jù)類型匹配的支持,Swift中switch...case語(yǔ)句支持多種數(shù)據(jù)類型的匹配判斷,十分強(qiáng)大,需要的朋友可以參考下2016-04-04NotificationCenter類實(shí)現(xiàn)原理
這篇文章主要為大家介紹了NotificationCenter類實(shí)現(xiàn)原理源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03Swift開(kāi)發(fā)之使用UIRefreshControl實(shí)現(xiàn)下拉刷新數(shù)據(jù)及uirefreshcontrol使用
本文給大家介紹使用UIRefreshControl實(shí)現(xiàn)下拉刷新數(shù)據(jù),及UIRefreshControl的使用步驟,對(duì)本文感興趣的朋友一起學(xué)習(xí)吧2015-11-11