文章标题
//注册请求
- (void)postRequest {
    //POST请求 请求参数放在请求内部(httpBody)     //设置请求
    NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
    request.timeoutInterval = 10;
    request.HTTPMethod = @"POST";
    request.URL = [NSURL URLWithString:@"https://api.cn.ronghub.com/user/getToken.json"];
    NSString * appkey = @"3argexb6rv2me";
    NSString * nonce = [NSString stringWithFormat:@"%d",arc4random()];
    NSString * timestamp = [[NSString alloc] initWithFormat:@"%ld",(NSInteger)[NSDate timeIntervalSinceReferenceDate]];   //配置http header
    [request setValue:appkey forHTTPHeaderField:@"App-Key"];
    [request setValue:nonce forHTTPHeaderField:@"Nonce"];
    [request setValue:timestamp forHTTPHeaderField:@"Timestamp"];
    [request setValue:@"SnJ02OMhhD2Yv" forHTTPHeaderField:@"appSecret"];     //生成hashcode 用以验证签名
    [request setValue:[self sha1:[NSString stringWithFormat:@"%@%@%@",appkey,nonce,timestamp]] forHTTPHeaderField:@"Signature"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    NSMutableDictionary * paramDic = [NSMutableDictionary dictionary];
    [paramDic setObject:@"graveyards1215@hotmail.com" forKey:@"userId"];
    [paramDic setObject:@"graveyards" forKey:@"name"];
    [paramDic setObject:@"1.png" forKey:@"portraitUri"];
    request.HTTPBody = [self httpBodyFromParamDictionary:paramDic];
    [NSURLConnection connectionWithRequest:request delegate:self];
}```

//hash算法
- (NSString*) sha1:(NSString *)hashString {
const char *cstr = [hashString cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:hashString.length];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, (CC_LONG)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;
}

//64位 hash算法
- (NSString ) sha1_base64:(NSString )hashString {
const char *cstr = [hashString cStringUsingEncoding:NSUTF8StringEncoding];
NSData *data = [NSData dataWithBytes:cstr length:hashString.length];
uint8_t digest[CC_SHA1_DIGEST_LENGTH];
CC_SHA1(data.bytes, (CC_LONG)data.length, digest);
NSData * base64 = [[NSData alloc]initWithBytes:digest length:CC_SHA1_DIGEST_LENGTH];
base64 = [GTMBase64 encodeData:base64];
NSString * output = [[NSString alloc] initWithData:base64 encoding:NSUTF8StringEncoding];
return output;
}

  • (NSData )httpBodyFromParamDictionary:(NSDictionary )param {
    NSMutableString * data = [NSMutableString string];
    for (NSString * key in param.allKeys) {
    [data appendFormat:@”%@=%@&”,key,param[key]];
    }
    return [[data substringToIndex:data.length-1] dataUsingEncoding:NSUTF8StringEncoding];
    }



  • (void)connection:(NSURLConnection )connection didReceiveData:(NSData )data
    {
    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
    [self loginRongCloud:@”graveyards1215@hotmail.com” token:[dic valueForKey:@”token”] password:@”tomorrow962464an”];

}

  • (void)loginRongCloud:(NSString )userName token:(NSString )token password:(NSString *)password
    {
    //登陆融云服务器
    [[RCIM sharedRCIM] connectWithToken:token success:^(NSString *userId) {
    NSLog([NSString stringWithFormat:@”token is %@ userId is %@”,token,userId],nil);
    } error:^(RCConnectErrorCode status) {
    //关闭HUD
    NSLog(@”RCConnectErrorCode is %ld”,(long)status);

    } tokenIncorrect:^{
    NSLog(@”IncorrectToken”);

    dispatch_async(dispatch_get_main_queue(), ^{
    });
    

    }];
    }

“`

阅读更多
个人分类: 融云-token-P
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

文章标题

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭