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


       最近一直在研究如何給app更多實用性的功能,在app進行登錄或者注冊時,為了方便用戶更加快捷的完成登錄功能,所以就決定采用短信驗證碼的方式進行驗證登錄。在學習的過程中,先使用了Mob的短信服務進行短信驗證,因為他是免費的,而且不需要提交什么材料(單純為了測試),后期加入到項目中的話,還是需要去創建自己的簽名和短信模板,先拿Mob練練手,后期在項目中還是會使用阿里雲的短信服務,到時候實現了之后也會分享出來。

      因為我這里只是做了一個簡單的Demo,所以就沒有必要去申請這些權限,其實申請的話很容易過的,只要上傳一下公司的營業執照就好。好了,廢話不多說了,實現功能吧!

      需要先在Mob上有一個自己的賬號,用自己的常用郵箱注冊就好,然后進入到后台,創建一個自己的應用,隨便取個名字就可以創建成功,創建成功后就可以拿到Appkey及Appsecret,這兩個數據后面是需要用到的,有了這個就可以非常方便的看到后台的統計信息,然后再Mob官網上下載SMSSDK,因為都是開源項目,所以Mob的代碼都是托管在git上的,然后將里面的Unity包導入到項目中。我新建了一個工程來實現該功能,利用UGUI搭建了一個簡易的收發驗證碼的界面。接下來就是開始碼代碼了,新建一個測試腳本,並且繼承且實現SMSSDKHandler接口,為了方便接收驗證碼發送的回調結果。先申明SMSSDK變量,然后在Start中初始化,將先前創建的Appkey及App secret填入,第三個參數為是否warn,根據官網建議設置為false。

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方法中返回驗證失敗。

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上了,有興趣的可以下載下來看看。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM