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
 
  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
 
  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];

你可能感兴趣的:(ios)