关闭

文章标题

182人阅读 评论(0) 收藏 举报
分类:
//注册请求
- (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

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:7571次
    • 积分:353
    • 等级:
    • 排名:千里之外
    • 原创:29篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档