最近公司用Unity开发项目,需要与iOS进行交互,主要是照相和验证码交互,所以写下心得,与大家互相学习。
验证码部分
由于只用到了腾讯云单发短信这个功能,所以我们可以看到腾讯云给定的模版:https://cloud.tencent.com/document/product/382/5976,里面参数介绍的很详细,再次不做叙述。
unity代码:
#if UNITY_IPHONE
[DllImport("__Internal")]
private static extern void sendSMS (string phoneNum,int appid,string appkey,int templateId,string smsSign);
对验证方法进行声明,这里我需要传给iOS那边传入参数。
//注解
phoneNum :接收验证码的手机号
appid:在腾讯云上申请到的sdkappid
appkey:sdkappid对应的appkey
templateId:在控制台审核通过的模板 ID
smsSign:短信签名
#elif UNITY_IPHONE && !UNITY_EDITOR
Debug.Log("苹果验证码");
sendSMS(PhoneNumText.text, UIManager.Instance.appid, UIManager.Instance.appkey, UIManager.Instance.templateId, UIManager.Instance.smsSign);
然后再需要的位置调用苹果发送验证码。
*使用 [DllImport("__Internal")]需要加入命名空间
using System.Runtime.InteropServices; //[DllImport]特性的命名空间。
当苹果向腾讯云发送完验证码后,需要向unity返回自己生成的验证码,这边就可以比对验证码是否输入有误
public void RecCode(string code)
{
this.code = code;
}
Xcode代码:
iOS这边需要做三件事情:1.接收unity传过来的参数 2.向腾讯云发送消息 3.接收消息后