雲のメッセージの音声、ショートメッセージの検証コードは実現します.
13518 ワード
クラウドメッセージの音声認証コード機能を使用して、クラウドのメッセージオープンプラットフォームに対応するアカウントを登録してから使用できます.
ここですhttp://www.ucpaas.com/doc/doc_rest 3-2.jsp 公式文書です.最初の行の文字を自分で研究する必要があります.
業務機能の実現点が分かります.
以下はパッケージされたクラスで、SDKを使用するヘルプクラスについて:
説明:ここで提供しているソースの一部は、良いコードを意味していません.具体的には公式文書を参照して説明してください.
ここですhttp://www.ucpaas.com/doc/doc_rest 3-2.jsp 公式文書です.最初の行の文字を自分で研究する必要があります.
業務機能の実現点が分かります.
以下はパッケージされたクラスで、SDKを使用するヘルプクラスについて:
//
// HYBUCSSDKHelper.h
// UCSVoiceOrSMSVerifyCodeDemo
//
// Created by on 15/2/2.
//
#import
#import "UCSEvent.h"
#import "UCSService.h"
/*!
* UCS SDK , IM、 、VOIP
* @author huangyibiao
*/
@interface HYBUCSSDKHelper : NSObject
/*!
*
*/
+ (HYBUCSSDKHelper *)shared;
/*!
*--------------------------------------------------------------------------
* , API
*--------------------------------------------------------------------------*/
//
- (NSInteger)connectWithClientNumber:(NSString *)clientNumber
password:(NSString *)clientPassword;
// ( )
- (NSInteger)connect:(NSString *)token;
// ( , IP,Port)
- (NSInteger)connect:(NSString *)hostAddress
port:(NSString *)hostPort
clientNumber:(NSString *)clientNumber
password:(NSString *)clientPassword;
// ( , IP,Port)
- (NSInteger)connect:(NSString *)hostAddress
withPort:(NSString *)hostPort
withToken:(NSString *)token;
//
- (BOOL)isConnected;
/*!
*--------------------------------------------------------------------------
* API
*--------------------------------------------------------------------------*/
/**
*
*/
- (void)ucsVerifyCodeWithPhone:(NSString *)phone
seconds:(int)seconds;
/**
*
*/
- (void)checkUcsVerifyCodeWithPhone:(NSString *)phone
verifycode:(NSString *)verifycode;
/*!
*--------------------------------------------------------------------------
* 、 API
*--------------------------------------------------------------------------*/
/**
*
*/
- (void)voiceCodeTo:(NSString *)phone verifyCode:(NSString *)verifyCode;
/**
*
* @param phone , , 100
* ( , , :0081)
* @param param , { }, ,
*/
- (void)smsCodeTo:(NSString *)phone param:(NSString *)param;
@end
以下は実現ファイルです.ここのネットワークライブラリはAFMを使います.#define kAccountSid @"" // sid
#define kAccountToken @""
#define kAppId @""
#define kAppName @""
#define kUCSBaseURL @"https://api.ucpaas.com"
#define kSoftVersion @"2014-06-30" // REST API 。
@interface HYBUCSSDKHelper ()
@property (nonatomic, strong) UCSService *ucsService;
@property (nonatomic, copy) NSString *callerPhoneNumber; //
@property (nonatomic, copy) NSString *phoneNumber; //
@end
@implementation HYBUCSSDKHelper
+ (HYBUCSSDKHelper *)shared {
static HYBUCSSDKHelper *s_helper = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
if (s_helper == nil) {
s_helper = [[[self class] alloc] init];
}
});
return s_helper;
}
- (instancetype)init {
if (self = [super init]) {
self.ucsService = [[UCSService alloc] initWithDelegate:self];
}
return self;
}
/*!
*--------------------------------------------------------------------------
* , API
*--------------------------------------------------------------------------*/
//
- (NSInteger)connectWithClientNumber:(NSString *)clientNumber password:(NSString *)clientPassword {
return [self.ucsService connect:kAccountSid
withAccountToken:kAccountToken
withClientNumber:clientNumber
withClientPwd:clientPassword];
}
// ( )
- (NSInteger)connect:(NSString *)token {
return [self.ucsService connect:token];
}
// ( , IP,Port)
- (NSInteger)connect:(NSString *)hostAddress
port:(NSString *)hostPort
clientNumber:(NSString *)clientNumber
password:(NSString *)clientPassword {
return [self.ucsService connect:hostAddress
withPort:hostPort
withwithAccountSid:kAccountSid
withAccountToken:kAccountToken
withClientNumber:clientNumber
withClientPwd:clientPassword];
}
// ( , IP,Port)
- (NSInteger)connect:(NSString *)hostAddress
withPort:(NSString *)hostPort
withToken:(NSString *)token {
return [self.ucsService connect:hostAddress withPort:hostPort withToken:token];
}
//
- (BOOL)isConnected {
return [self.ucsService isConnected];
}
/*!
*--------------------------------------------------------------------------
*
*--------------------------------------------------------------------------*/
/**
*
*/
- (void)ucsVerifyCodeWithPhone:(NSString *)phone
seconds:(int)seconds {
[self.ucsService getVerificationCode:kAccountSid
withAppid:kAppId
withAppName:kAppName
withCodetype:1
withPhone:phone
withSeconds:seconds
withBusiness:1];
}
/**
*
*/
- (void)checkUcsVerifyCodeWithPhone:(NSString *)phone
verifycode:(NSString *)verifycode {
[self.ucsService doVerificationCode:kAccountSid
withAppid:kAppId
withPhone:phone
withVerifycode:verifycode];
}
/*!
*--------------------------------------------------------------------------
* 、 API
*--------------------------------------------------------------------------*/
/**
*
*/
- (void)voiceCodeTo:(NSString *)phone verifyCode:(NSString *)verifyCode {
AFHTTPRequestOperationManager *manager = [self operationManagerWithBaseUrl:kUCSBaseURL];
//
NSString *timestamp = [self timestamp];
[manager.requestSerializer setValue:[self authorization:timestamp] forHTTPHeaderField:@"Authorization"];
NSString *url = [NSString stringWithFormat:@"/%@/Accounts/%@/Calls/voiceCode?sig=%@",
kSoftVersion, kAccountSid, [self sig:timestamp]];
NSDictionary *params = @{@"voiceCode" : @{@"appId" : kAppId,
@"to" : phone,
@"verifyCode" : verifyCode}};
[manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"url: %@
parmas: %@
responseObject:%@
errorMessage: %@ errorCode=%@ headers:%@",
operation.response.URL.absoluteString,
params,
responseObject,
responseObject[@"errorMessage"],
responseObject[@"errorCode"],
operation.request.allHTTPHeaderFields);
if ([responseObject isKindOfClass:[NSDictionary class]]) {
NSLog(@"%@", responseObject);
} else {
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", [error description]);
}];
}
- (NSString *)timestamp {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyyMMddHHmmss"];
NSDate *datenow = [NSDate date];
NSString *timestamp = [formatter stringFromDate:datenow];
return timestamp;
}
- (NSString *)authorization:(NSString *)timestamp {
// Base64 ( Id + + )
NSString *authorization = [NSString stringWithFormat:@"%@:%@", kAccountSid, timestamp];
authorization = [self base64Encoding:authorization];
return authorization;
}
- (NSString *)sig:(NSString *)timestamp {
// URL sig ,sig= MD5( Id + + ), 32 ( : )
// MD5 ( Id + + ), 32 。
NSString *sig = [NSString stringWithFormat:@"%@%@%@", kAccountSid, kAccountToken, timestamp];
sig = [[self md5n:sig] uppercaseString];
return sig;
}
/**
*
* @param phone , , 100
* ( , , :0081)
* @param param , { }, ,
*/
- (void)smsCodeTo:(NSString *)phone param:(NSString *)param {
AFHTTPRequestOperationManager *manager = [self operationManagerWithBaseUrl:kUCSBaseURL];
//
NSString *timestamp = [self timestamp];
[manager.requestSerializer setValue:[self authorization:timestamp] forHTTPHeaderField:@"Authorization"];
NSString *url = [NSString stringWithFormat:@"/%@/Accounts/%@/Messages/templateSMS?sig=%@",
kSoftVersion, kAccountSid, [self sig:timestamp]];
NSDictionary *params = @{@"templateSMS" : @{@"appId" : kAppId,
@"to" : phone,
@"templateId" : @(3177),
@"param" : param}};
[manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"url: %@
parmas: %@
responseObject:%@
errorMessage: %@ errorCode=%@ headers:%@",
operation.response.URL.absoluteString,
params,
responseObject,
responseObject[@"errorMessage"],
responseObject[@"errorCode"],
operation.request.allHTTPHeaderFields);
if ([responseObject isKindOfClass:[NSDictionary class]]) {
NSLog(@"%@", responseObject);
} else {
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", [error description]);
}];
}
/*!
* @brief , base64
* @return base64
*/
- (NSString *)base64Encoding:(NSString *)str {
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
NSString *result = nil;
// ios7
#define kIsIOS7OrLater ([UIDevice currentDevice].systemVersion.integerValue >= 7 ? YES : NO)
if (kIsIOS7OrLater) {
result = [data base64EncodedStringWithOptions:0];
} else {
result = [data base64Encoding];
}
return result;
}
#pragma mark - md5
/*!
* @brief md5 ,
* @return
*/
- (NSString *)md5n:(NSString *)str {
const char *cStr = [str UTF8String];
unsigned char result[16];
CC_MD5(cStr, strlen(cStr), result);
return [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]];
}
#pragma mark - UCCEventDelegate
//
- (void)onConnectionSuccessful:(NSInteger)result {
NSLog(@" result=%ld", result);
}
//
- (void)onConnectionFailed:(NSInteger)reason {
NSLog(@" reason=%ld", reason);
}
/*!
*--------------------------------------------------------------------------
* : 、 API
*--------------------------------------------------------------------------*/
// 0: ,1:
- (void) onGetValiateCodeSuccessful:(NSInteger)nResult {
NSLog(@"%@", nResult ? @" " : @" ");
}
//
- (void) onGetValiateCodeFailed:(NSInteger)reason {
NSLog(@" reason=%ld", reason);
}
//
- (void) onDoValiateCodeSuccessful:(NSInteger)nResult {
NSLog(@" result=%ld", nResult);
}
//
- (void) onDoValiateCodeFailed:(NSInteger)reason {
NSLog(@" reason=%ld", reason);
}
- (AFHTTPRequestOperationManager *)operationManagerWithBaseUrl:(NSString *)url {
//
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES;
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc]
initWithBaseURL:[NSURL URLWithString:url]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.requestSerializer.stringEncoding = NSUTF8StringEncoding;
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[manager.requestSerializer setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"256" forHTTPHeaderField:@"Content-Length"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[@"application/json",
@"text/html",
@"text/json",
@"text/javascript"]];
return manager;
}
@end
上の部分のソースは無駄です.これは後の追加機能のために追加されたものです.ここでは音声、ショートメッセージの検証コード機能の実現だけを説明します.説明:ここで提供しているソースの一部は、良いコードを意味していません.具体的には公式文書を参照して説明してください.