参考: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);
}
}];
}
}];
}
}];
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];