IOS: How to authenticate the GKLocalPlayer on my 'third party server'.

参考:http://stackoverflow.com/questions/17408729/how-to-authenticate-the-gklocalplayer-on-my-third-party-server

https://gist.github.com/andyzinsser/8044165


工作总结:

任务说明:使用GameCenter账户作为游戏的账户。需要在本地服务器上验证登入信息。

主要参考https://gist.github.com/andyzinsser/8044165网站代码。

1.登入后给自己游戏服务器发post请求确认数据信息

流程:登入GameCenter-通过generateIdentityVerificationSignatureWithCompletionHandler获取相关信息-

[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {
        if (error) {
            NSLog(@"ERROR: %@",error);
        }
        else
        {
            [[GKLocalPlayer localPlayer] generateIdentityVerificationSignatureWithCompletionHandler:^(NSURL *publicKeyUrl, NSData *signature, NSData *salt, uint64_t timestamp, NSError *error) {
                if (error) {
                    NSLog(@"ERROR: %@",error);
                }
                else{
                    // package data to be sent to server for verification
                    NSDictionary *params = @{@"public_key_url": [publicKeyUrl absoluteString],
                                             @"timestamp": [NSString stringWithFormat:@"%llu", timestamp],
                                             @"signature": [signature base64EncodedStringWithOptions:0],
                                             @"salt": [salt base64EncodedStringWithOptions:0],
                                             @"player_id": [GKLocalPlayer localPlayer].playerID,
                                             @"app_bundle_id": [[NSBundle mainBundle] bundleIdentifier]};
                    NSError *JSONError = nil;
                    NSData *JSONPOSTData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:&JSONError];
                    NSString *loginURL = /*your game sever*/;
                    NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:loginURL]];
                    [postRequest setHTTPBody:JSONPOSTData];
                    [postRequest setHTTPMethod:@"POST"];
                    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
                    [NSURLConnection sendAsynchronousRequest:postRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                        if (error) {
                            NSLog(@"HttpPostError%@",error);
                        }
                        else
                        {
                            NSInteger responseCode = [(NSHTTPURLResponse*)response statusCode];
                            NSString *responseString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
                            NSLog(@"HttpResponseCode:%d",responseCode);
                            NSLog(@"HttpResponseString:%@",responseString);
                        }
                    }];
                }
                
            }];
        }
    }];

2. NSDictionary 转 JSON
NSDictionary *params = @{@"public_key_url": [publicKeyUrl absoluteString],
                                             @"timestamp": [NSString stringWithFormat:@"%llu", timestamp],
                                             @"signature": [signature base64EncodedStringWithOptions:0],
                                             @"salt": [salt base64EncodedStringWithOptions:0],
                                             @"player_id": [GKLocalPlayer localPlayer].playerID,
                                             @"app_bundle_id": [[NSBundle mainBundle] bundleIdentifier]};
                    NSError *JSONError = nil;
                    NSData *JSONPOSTData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:&JSONError];

2.JSON 转 NSString

NSString *JSONPOSTString = [[NSString alloc] initWithData:JSONPOSTData encoding:NSUTF8StringEncoding];

3.JSON 转 NSDictionary

NSError *error = nil;

NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值