SMSSDK接入(Mob短信SDK接入)

    教程的话 AndroidSDK官方文档 里面有但是对像我这样的小白来说看不懂 ,百度了老半天才搞出来,所以特地写一下少让像我一样的小白少走弯路。        

    我目前知道的SDK有Mob和易接两个平台,这篇只说Mob中的SMSSDK(短信SDK)。

先去Mob官网注册一个账号,然后点击添加应用,在添加SMSSDK。



然后点击SMSSDK 再点概况  在这个里面能看到自己的App Key和App Serect  


1、下载SMSSDK的Unity3D的工具类

打开Github下载Unity3D-For-SMSSDK项目。将项目中的Unity3DForSMSSDK/Assets/Plugins目录拷贝到您的项目的Assets目录中,或双击SMSSDKPackageForUnity.unitypackage导入相关文件。注意该操作可能会覆盖您原来已经存在的文件!

2、挂接SMSSDK脚本

选择好需要挂接的GameObject(例如MainCamera),在右侧栏中点击Add Component,选择SMSSDK 进行挂接。如果需要使用Demo.cs文件,也需要进行挂接主相机。方法同挂接SMSSDK相同。

然后打开这个挂载的脚本把里面的所有的App Key和App Serect都改成自己的。

3.写一个自己的脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using cn.SMSSDK.Unity;

public class Test : MonoBehaviour, SMSSDKHandler
{
    public InputField PhoneNumberInput;
    public InputField VerificationInput;

    public Text respText;
    public Text MessText;

  

    private SMSSDK smssdk;

    private UserInfo userInfo;

    private string phoneNum = "";
    private string zone = "86";

    private string appKey = "26332c93b4dca";
    private string appSerect = "d1a6da44bdcd299b690327b008318bfd";

    // Use this for initialization    
    void Start()
    {
        smssdk = GameObject.Find("Main Camera").GetComponent<SMSSDK>();
        smssdk.init(appKey, appSerect, false);//初始化SDK    

        smssdk.setHandler(this);//设置回调,用户处理从客户端返回的信息    

        userInfo = new UserInfo();
    }

    // Update is called once per frame    
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            Application.Quit();
        }
    }

    public void OnBtnVerification()
    {
        phoneNum = PhoneNumberInput.text;
        Debug.Log(phoneNum);
        //通过手机号获取验证码    
        smssdk.getCode(CodeType.TextCode, phoneNum, zone, null);
    }

    public void OnBtnOK()
    {
        //提交验证码    
        smssdk.commitCode(phoneNum, zone, VerificationInput.text);
    }

    /// <summary>
    ///   点击获取验证码时回   resp返回false    action返回 “isSmart”   当验证码正确的时候会返回 手机号还有国际区号
    /// </summary>
    /// <param name="action"></param>
    /// <param name="resp"></param>
    public void onComplete(int action, object resp)
    {
       
        ActionType act = (ActionType)action;
        if (resp != null)
        {
            respText.text = resp.ToString();
        }

        //一下代码都是提示

        if (act == ActionType.GetCode)
        {
            string responseString = (string)resp;
            Debug.Log("isSmart :" + responseString);
            MessText.text = "isSmart :" + responseString;
        }
        else if (act == ActionType.GetVersion)
        {
            string version = (string)resp;
            MessText.text = "version :" + version;
        }
        else if (act == ActionType.GetSupportedCountries)
        {

            string responseString = (string)resp;
            Debug.Log("zoneString :" + responseString);
            MessText.text = "zoneString :" + responseString;

        }
        else if (act == ActionType.GetFriends)
        {
            string responseString = (string)resp;
            Debug.Log("friendsString :" + responseString);
            MessText.text = "friendsString :" + responseString;
        }
        else if (act == ActionType.CommitCode)
        {

            string responseString = (string)resp;
            Debug.Log("commitCodeString :" + responseString);
            MessText.text = "commitCodeString :" + responseString;

        }
        else if (act == ActionType.SubmitUserInfo)
        {

            string responseString = (string)resp;
            Debug.Log("submitString :" + responseString);
            MessText.text = "submitString :" + responseString;
        }
        else if (act == ActionType.ShowRegisterView)
        {

            string responseString = (string)resp;
            Debug.Log("showRegisterView :" + responseString);
            MessText.text = "showRegisterView :" + responseString;
        }
        else if (act == ActionType.ShowContractFriendsView)
        {

            string responseString = (string)resp;
            Debug.Log("showContractFriendsView :" + responseString);
            MessText.text = "showContractFriendsView :" + responseString;
        }
    }

    /// <summary>
    /// 发生错误时回调用此方法
    /// </summary>
    /// <param name="action"></param>
    /// <param name="resp"></param>
    public void onError(int action, object resp)
    {
       
        Debug.Log("Error :" + resp);
        respText.text = resp.ToString();
    }
}

大家一定要打包到手机里测试,也不要用模拟器测试。


下图为验证成功的图片:

主要是让大家看看成功以后的返回值



在下也是小白,肯定有不足的地方,大家多多交流互相提高。

猜你喜欢

转载自blog.csdn.net/Yuan_bowen/article/details/80582972