先看一下官方給出的API簽名規(guī)則
然后是簽名請(qǐng)求參數(shù)準(zhǔn)備,新建一個(gè)MSTool類虏等,并導(dǎo)入頭文件#import <CommonCrypto/CommonCrypto.h>
代碼如下:
#import "MSTool.h"
#import <CommonCrypto/CommonCrypto.h>
@implementation MSTool
+ (instancetype)sharedMSTool{
static MSTool * tool = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
tool = [[self alloc] init];
});
return tool;
}
//single_implementation(MSTool)
//獲取隨機(jī)數(shù)
-(NSString *)getRandomNonce
{
NSInteger randomValue = [self getRandomNumber:100000 to:999999];
return [NSString stringWithFormat:@"%ld",randomValue];
}
//獲取時(shí)間戳 從1970年
-(NSString *)getTimestamp
{
NSDate *date = [NSDate date];
NSTimeInterval times = [date timeIntervalSince1970];
return [NSString stringWithFormat:@"%.0f",times];
}
//獲取從 from 到 to 的隨機(jī)數(shù)
-(NSInteger)getRandomNumber:(NSInteger)from to:(NSInteger)to
{
return (NSInteger)(from + (arc4random() % (to - from + 1)));
}
//sha1 加密
-(NSString *)sha1WithKey:(NSString *)key
{
const char *cstr = [key cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:key.length];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, data.length, digest);
NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 2];
for(int i=0; i<CC_SHA1_DIGEST_LENGTH; i++) {
[output appendFormat:@"%02x", digest[i]];
}
return output;
}
//根據(jù)appSecret nonce timestamp 獲取signature
-(NSString *)getSignatureWithAppSecret:(NSString *)appSecret nonce:(NSString *)nonce timestamp:(NSString *)timestamp
{
NSString *sha1String = [NSString stringWithFormat:@"%@%@%@",appSecret,nonce,timestamp];
return [self sha1WithKey:sha1String];
}
@end
然后是在Appdelegate中設(shè)置
#pragma mark - 融云客服設(shè)置
- (void)getToken{
NSString * url = @"https://api.cn.ronghub.com/user/getToken.json";
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
NSDictionary *dict = @{@"userId":請(qǐng)求用戶唯一標(biāo)識(shí)符@"name":用戶名,@"portraiUri":圖片的網(wǎng)址"};
NSString *appkey = @"你在融云申請(qǐng)的App Key";
NSString *nonce = [[MSTool sharedMSTool] getRandomNonce];
NSString *timestamp = [[MSTool sharedMSTool] getTimestamp];
NSString *signature = [[MSTool sharedMSTool]getSignatureWithAppSecret:@"你在融云申請(qǐng)的AppSecret" nonce:nonce timestamp:timestamp];
//設(shè)置請(qǐng)求頭
[manager.requestSerializer setValue:appkey forHTTPHeaderField:@"App-Key"];
[manager.requestSerializer setValue:nonce forHTTPHeaderField:@"Nonce"];
[manager.requestSerializer setValue:timestamp forHTTPHeaderField:@"Timestamp"];
[manager.requestSerializer setValue:signature forHTTPHeaderField:@"Signature"];
//調(diào)用POST方法
[manager POST:url parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"??%@",responseObject);
//客服設(shè)置
[[RCIM sharedRCIM]initWithAppKey:@"你在融云申請(qǐng)的App Key"];
[[RCIM sharedRCIM] connectWithToken:responseObject[@"token"] success:^(NSString *userId) {
NSLog(@"LoginSuccess");
} error:^(RCConnectErrorCode status) {
NSLog(@"LoginFault");
} tokenIncorrect:^{
NSLog(@"Token_isEorro");
}];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}