//發送短信驗證碼URL NSString *httpSmsAuthCodeUrl = @"http://<ip>:<port>/open/api/sms/authcode/send"; //校驗短信驗證碼URL NSString *httpVerifySmsAuthCodeUrl = @"http://<ip>:<port>/open/api/sms/authcode/verify"; #pragma mark 獲取短信驗證碼 - (void)sendSmsAuthCode{ NSLog(@"go"); //短信驗證碼URL請求轉換爲NSURL NSURL *url = [NSURL URLWithString: httpSmsAuthCodeUrl]; //建立可變http請求對象 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0]; //POST方式發起 [request setHTTPMethod:@"POST"]; //添加Content-type頭域 [request setValue:kHttpHeaderContentTypeValue forHTTPHeaderField:kHttpHeaderContentType]; //HTTP報體數據字典 NSDictionary *dict = @{@"apiKey": @"357964ca9d6147a38746798586994a93", @"secretKey": @"a2ef78b6d4e440239b8789856c86e9a5", @"sourceAddr": @"1069060600860007", @"destAddr": @"18867101821", @"expireSeconds": @"300"}; //字典轉換成NSDATA類型 NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil]; //設置HPPT報體 [request setHTTPBody:data]; //發起同步請求,超時時間在建立HTTP請求對象時設置 NSURLResponse *response = nil; NSError *error = nil; //response包含HTTP請求URL,HTTP應答狀態碼,HTTP應答頭域 //dataResp包含HTTP應答報體 NSData *dataResp = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if (error) { NSLog(@"error: %@", error); return; } //dataResp轉成字典,以便獲取應答字段的內容 NSDictionary *dictResp = [NSJSONSerialization JSONObjectWithData:dataResp options:NSJSONReadingAllowFragments error:&error]; NSMutableString *result = [[NSMutableString alloc]initWithFormat:@"%@: %@", dictResp[@"resultCode"], dictResp[@"resultMsg"]]; //彈提示框,告知驗證結果 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"獲取短信驗證碼結果" message:result delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil]; [alert show]; return; }
#pragma mark 校驗短信驗證碼 - (void)verifySmsAuthCode{ NSString *smsAuthCode = self.smsText.text; //校驗短信驗證碼URL請求轉換爲NSURL NSURL *url = [NSURL URLWithString: httpVerifySmsAuthCodeUrl]; //建立可變http請求對象 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0]; //POST方式發起 [request setHTTPMethod:@"POST"]; //添加Content-type頭域 [request setValue:kHttpHeaderContentTypeValue forHTTPHeaderField:kHttpHeaderContentType]; //HTTP報體數據字典 NSDictionary *dict = @{@"apiKey": @"357964ca9d6147a38746798586994a93", @"destAddr": @"18867101821", @"authCode": smsAuthCode}; //字典轉換成NSDATA類型 NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:nil]; //設置HPPT報體 [request setHTTPBody:data]; //發起同步請求,超時時間在建立HTTP請求對象時設置 NSURLResponse *response = nil; NSError *error = nil; //response包含HTTP請求URL,HTTP應答狀態碼,HTTP應答頭域 //dataResp包含HTTP應答報體 NSData *dataResp = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; if (error) { NSLog(@"error: %@", error); return; } //dataResp轉成字典,以便獲取應答字段的內容 NSDictionary *dictResp = [NSJSONSerialization JSONObjectWithData:dataResp options:NSJSONReadingAllowFragments error:&error]; NSMutableString *result = [[NSMutableString alloc]initWithFormat:@"%@: %@", dictResp[@"resultCode"], dictResp[@"resultMsg"]]; //彈提示框,告知驗證結果 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"驗證結果" message:result delegate:self cancelButtonTitle:@"肯定" otherButtonTitles:nil, nil]; [alert show]; return; }
中移貫衆開放平臺短信驗證碼開發說明:api
http://openservice.com.cn/open_devportal/authcode.actionurl
運行效果以下圖所示。code
點擊圖標獲取驗證碼。orm
輸入驗證碼後,點擊驗證,會獲得驗證結果。對象