Unity利用SMSSDK實現短信驗證碼(附代碼)

       最近一直在研究如何給app更多實用性的功能,在app進行登陸或者註冊時,爲了方便用戶更加快捷的完成登陸功能,因此就決定採用短信驗證碼的方式進行驗證登陸。在學習的過程當中,先使用了Mob的短信服務進行短信驗證,由於他是免費的,並且不須要提交什麼材料(單純爲了測試),後期加入到項目中的話,仍是須要去建立本身的簽名和短信模板,先拿Mob練練手,後期在項目中仍是會使用阿里雲的短信服務,到時候實現了以後也會分享出來。git

      由於我這裏只是作了一個簡單的Demo,因此就沒有必要去申請這些權限,其實申請的話很容易過的,只要上傳一下公司的營業執照就好。好了,廢話很少說了,實現功能吧!github

      須要先在Mob上有一個本身的帳號,用本身的經常使用郵箱註冊就好,而後進入到後臺,建立一個本身的應用,隨便取個名字就能夠建立成功,建立成功後就能夠拿到Appkey及Appsecret,這兩個數據後面是須要用到的,有了這個就能夠很是方便的看到後臺的統計信息,而後再Mob官網上下載SMSSDK,由於都是開源項目,因此Mob的代碼都是託管在git上的,而後將裏面的Unity包導入到項目中。我新建了一個工程來實現該功能,利用UGUI搭建了一個簡易的收發驗證碼的界面。接下來就是開始碼代碼了,新建一個測試腳本,而且繼承且實現SMSSDKHandler接口,爲了方便接收驗證碼發送的回調結果。先申明SMSSDK變量,而後在Start中初始化,將先前建立的Appkey及App secret填入,第三個參數爲是否warn,根據官網建議設置爲false。app

ssdk.init("292449f735890", "f1bee8045aac2e6cbb7c535a5277aa1c", false); ssdk.setHandler(this);

      接下來是實現短信驗證碼功能,特別須要注意的是第四個參數,它表示的短信模板,由於咱們一開始是沒有申請到短信模板的,由於Mob須要咱們的應用中先利用他們sdk實現了短信驗證碼功能,提交的app才能經過審覈。因此此時咱們是沒有短信模板的,因此這裏在測試的時候傳null就行了。學習

ssdk.getCode(CodeType.TextCode, phone, "86", null);

      而後點擊發送按鈕後,就能夠接收到短信了,接下來就是驗證驗證碼是否正確了。phoneNumber表示的是手機號,codeNumer表示的是輸入的驗證碼,點擊驗證後,就會自動驗證了。測試

ssdk.commitCode("phoneNumber", "86", "codeNumber");

       前面由於咱們實現了SMSSDKHandler接口,因此在onComplete方法中返回驗證成功,在onError方法中返回驗證失敗。this

public void onComplete(int action, object resp) { ActionType act = (ActionType)action; if (resp != null) { //result = resp.ToString(); text.text += "\n" + resp.ToString(); Debug.Log(resp.ToString()); } if (act == ActionType.GetCode) { text.text += "\n 驗證成功!!!"; string responseString = (string)resp; Debug.Log("isSmart :" + responseString); } } public void onError(int action, object resp) { Debug.Log("Error :" + resp); text.text += "\n 驗證失敗!!!"; text.text += "\n Error : " + resp; print("OnError ******resp" + resp); }

       如下是個人完整代碼。阿里雲

public class Test : MonoBehaviour, SMSSDKHandler { public SMSSDK ssdk; private InputField code; private InputField phoneNum; private Button enter; private Button send; private string codeNum; private string phone; private Text timer; private bool isSend; private int time; private float t; public Text text; private void Start() { ssdk.init("292449f735890", "f1bee8045aac2e6cbb7c535a5277aa1c", false); ssdk.setHandler(this); timer = transform.Find("Timer").GetComponent<Text>(); code = transform.Find("code").GetComponent<InputField>(); phoneNum = transform.Find("num").GetComponent<InputField>(); enter = transform.Find("enter").GetComponent<Button>(); send = transform.Find("send").GetComponent<Button>(); timer.gameObject.SetActive(false); enter.onClick.AddListener(EnterCodeHandler); send.onClick.AddListener(SendCodeHandler); } private void Update() { if (isSend) { //倒計時 timer.text = time.ToString(); t += Time.deltaTime; if (t >= 1) { time--; t = 0; } if (time < 0) { isSend = false; send.gameObject.SetActive(true); timer.gameObject.SetActive(false); } } } /// <summary> /// 發送驗證碼 /// </summary> private void SendCodeHandler() { phone = phoneNum.text; isSend = true; time = 60; send.gameObject.SetActive(false); timer.gameObject.SetActive(true); ssdk.getCode(CodeType.TextCode, phone, "86", null); } /// <summary> /// 點擊肯定,對比驗證碼 /// </summary> private void EnterCodeHandler() { ssdk.commitCode(phone, "86", code.text); } public void onComplete(int action, object resp) { ActionType act = (ActionType)action; if (resp != null) { //result = resp.ToString(); text.text += "\n" + resp.ToString(); Debug.Log(resp.ToString()); } if (act == ActionType.GetCode) { text.text += "\n 驗證成功!!!"; string responseString = (string)resp; Debug.Log("isSmart :" + responseString); } } public void onError(int action, object resp) { Debug.Log("Error :" + resp); text.text += "\n 驗證失敗!!!"; text.text += "\n Error : " + resp; print("OnError ******resp" + resp); } }

       經過上面的實現,咱們如今就基本實現了短信驗證功能,可是由於Mob是免費的,因此對每一個手機號都有限制,好像是對每個手機號都只能驗證一次,因此很不方便。接下來我打算利用阿里雲的短信服務實現一個,到時候也會分享出來,這個項目的源碼及我發佈的一個測試版本都放在了個人https://github.com/Iamdevelope/SMSSDemo上了,有興趣的能夠下載下來看看。spa

相關文章
相關標籤/搜索