iOS gamecenter login实现

出自:http://blog.csdn.net/sleepingboy888/article/details/19967625

参考: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获取相关信息-

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) {  
  2.         if (error) {  
  3.             NSLog(@"ERROR: %@",error);  
  4.         }  
  5.         else  
  6.         {  
  7.             [[GKLocalPlayer localPlayer] generateIdentityVerificationSignatureWithCompletionHandler:^(NSURL *publicKeyUrl, NSData *signature, NSData *salt, uint64_t timestamp, NSError *error) {  
  8.                 if (error) {  
  9.                     NSLog(@"ERROR: %@",error);  
  10.                 }  
  11.                 else{  
  12.                     // package data to be sent to server for verification  
  13.                     NSDictionary *params = @{@"public_key_url": [publicKeyUrl absoluteString],  
  14.                                              @"timestamp": [NSString stringWithFormat:@"%llu", timestamp],  
  15.                                              @"signature": [signature base64EncodedStringWithOptions:0],  
  16.                                              @"salt": [salt base64EncodedStringWithOptions:0],  
  17.                                              @"player_id": [GKLocalPlayer localPlayer].playerID,  
  18.                                              @"app_bundle_id": [[NSBundle mainBundle] bundleIdentifier]};  
  19.                     NSError *JSONError = nil;  
  20.                     NSData *JSONPOSTData = [NSJSONSerialization dataWithJSONObject:params options:NSJSONWritingPrettyPrinted error:&JSONError];  
  21.                     NSString *loginURL = /*your game sever*/;  
  22.                     NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:loginURL]];  
  23.                     [postRequest setHTTPBody:JSONPOSTData];  
  24.                     [postRequest setHTTPMethod:@"POST"];  
  25.                     NSOperationQueue *queue = [[NSOperationQueue alloc] init];  
  26.                     [NSURLConnection sendAsynchronousRequest:postRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {  
  27.                         if (error) {  
  28.                             NSLog(@"HttpPostError%@",error);  
  29.                         }  
  30.                         else  
  31.                         {  
  32.                             NSInteger responseCode = [(NSHTTPURLResponse*)response statusCode];  
  33.                             NSString *responseString = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];  
  34.                             NSLog(@"HttpResponseCode:%d",responseCode);  
  35.                             NSLog(@"HttpResponseString:%@",responseString);  
  36.                         }  
  37.                     }];  
  38.                 }  
  39.                   
  40.             }];  
  41.         }  
  42.     }];  

2. NSDictionary 转 JSON
[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. NSDictionary *params = @{@"public_key_url": [publicKeyUrl absoluteString],  
  2.                                              @"timestamp": [NSString stringWithFormat:@"%llu", timestamp],  
  3.                                              @"signature": [signature base64EncodedStringWithOptions:0],  
  4.                                              @"salt": [salt base64EncodedStringWithOptions:0],  
  5.                                              @"player_id": [GKLocalPlayer localPlayer].playerID,  
  6.                                              @"app_bundle_id": [[NSBundle mainBundle] bundleIdentifier]};  
  7.                     NSError *JSONError = nil;  
  8.                     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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值