Swift免費(fèi)短信驗(yàn)證碼實(shí)現(xiàn)及動(dòng)態(tài)倒計(jì)時(shí)功能
今天給大家?guī)硪粋€(gè)簡單的免費(fèi)短信驗(yàn)證碼實(shí)現(xiàn)demo,采用mob的短信驗(yàn)證碼SDK,到目前為止還是免費(fèi)的,只需要簡單的注冊(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編寫的,在與Swift結(jié)合時(shí),需要添加橋接文件,具體操作如下:
右鍵你的Swift工程,新建一個(gè)OC文件,名字隨便起,這時(shí)會(huì)彈出提示你創(chuàng)建一個(gè)橋接文件,點(diǎn)擊是就OK了!在你的工程中會(huì)多出一個(gè)以工程名--Bridging-Header.h的文件,打開寫入下面的代碼:
#import <SMS_SDK/SMSSDK.h>
當(dāng)然,創(chuàng)建橋接文件的方法有很多種,會(huì)的就無需關(guān)注咯。
2、打開工程中的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、編寫ViewController.swift,具體就看代碼吧,很簡單的一個(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ì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
實(shí)例講解Swift中引用類型的ARC自動(dòng)引用計(jì)數(shù)
自動(dòng)引用計(jì)數(shù)是在Objective-C中就有的特性,用來輔助管理對(duì)象的引用,這里我們就來以實(shí)例講解Swift中引用類型的ARC自動(dòng)引用計(jì)數(shù):2016-07-07
Swift實(shí)現(xiàn)監(jiān)聽鍵盤通知及一些處理詳解
這篇文章主要給大家介紹了關(guān)于Swift實(shí)現(xiàn)監(jiān)聽鍵盤通知及一些處理的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
swift 3.0 實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)功能
這篇文章主要介紹了swift 3.0 實(shí)現(xiàn)短信驗(yàn)證碼倒計(jì)時(shí)功能的相關(guān)資料,需要的朋友可以參考下2017-02-02
Swift仿選擇電影票的效果并實(shí)現(xiàn)無限/自動(dòng)輪播的方法
這篇文章主要給大家介紹了關(guān)于Swift仿選擇電影票的效果并實(shí)現(xiàn)無限/自動(dòng)輪播的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08

