標籤:
最近有學習一個小技能,這裡小結一下,分享給大家,互相交流。
首先是大體步驟:
- 在mob官網註冊,然後添加簡訊驗證的應用
- 使用cocoapods匯入架構
Podfile檔案:platform :ios, "6.0"target ‘簡訊驗證‘do# Mob產品公用庫
pod ‘MOBFoundation_IDFA‘
# SMSSDK必須pod ‘SMSSDK‘end
3.在AppDelegate註冊應用AppKey
4.擷取驗證碼
5.提交驗證碼
6.注意點:適配要記得開啟https
1.在AppDelegate中
// 註冊AppKey [SMSSDK registerApp:@"14810905c09b0" withSecret:@"fbea3e77174d8c9da1d0839b1f0bdc82"];
如:
2.簡訊和語言各一個簡單方法即可實現
/** * 簡訊驗證碼 */- (IBAction)messageVerify { // 驗證電話號碼是否合法 NSString *phoneNumber = self.phoneNumber.text; if (![phoneNumber isPhone]) { NSLog(@"你輸入電話號碼不正確"); return; } self.savePhoneNumber = phoneNumber; // 擷取簡訊驗證碼 [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:phoneNumber zone:@"86" customIdentifier:nil result:^(NSError *error) { if (error) { NSLog(@"擷取驗證碼失敗:%@",error); }else{ NSLog(@"擷取驗證碼成功"); } }];}/** * 語言驗證碼 */- (IBAction)voiceVerify { // 驗證電話號碼是否合法 NSString *phoneNumber = self.phoneNumber.text; if (![phoneNumber isPhone]) { NSLog(@"你輸入電話號碼不正確"); return; } self.savePhoneNumber = phoneNumber; // 擷取語音驗證碼 [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodVoice phoneNumber:phoneNumber zone:@"86" customIdentifier:nil result:^(NSError *error) { if (error) { NSLog(@"擷取驗證碼失敗:%@",error); }else{ NSLog(@"擷取驗證碼成功"); } }];}/** * 驗證 */- (IBAction)verifyResult { // 驗證驗證碼是否合法 NSString *verifyCode = self.verifyLabel.text; if (verifyCode.length != 4) { NSLog(@"驗證輸入錯誤"); return; } // 提交驗證碼 [SMSSDK commitVerificationCode:verifyCode phoneNumber:self.savePhoneNumber zone:@"86" result:^(NSError *error) { if (!error) { NSLog(@"驗證成功"); }else{ NSLog(@"錯誤資訊:%@",error); } }];}
上例是使用mob預設的簡訊格式,也可以自訂,可根據自身情況來選擇。
其他還有一些修改,大家可以閱讀mob上的sdk文檔來整合自己需要的形式。
iOS使用技能 - 簡訊,語言驗證碼的擷取與驗證小結