文章标题

//注册请求
- (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(), ^{
    });
    

    }];
    }

“`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值