gpt4 book ai didi

ios - 3G 连接的 GameKit 配对失败

转载 作者:IT王子 更新时间:2023-10-29 07:43:36 24 4
gpt4 key购买 nike

我正在为 iOS 制作一款多人游戏,我阅读了 Apple Developer Center 中的资料,特别是 this one .这是我的自定义配对代码,非常简单:

- (void)findProgrammaticMatch {

GKMatchRequest *request = [[GKMatchRequest alloc] init];
request.minPlayers = 2;
request.maxPlayers = 2;
request.defaultNumberOfPlayers = 2;
request.playersToInvite = nil;
request.playerAttributes = 0;
request.playerGroup = 0;

UILabel *loading = (UILabel *)[aiw viewWithTag:792];

[[GKMatchmaker sharedMatchmaker] findMatchForRequest:request withCompletionHandler:^(GKMatch *match, NSError *error) {
if (error){

//error handling
[loaderLayer stopAnimating];
UIButton *cancelButton = (UIButton *)[loaderLayer viewWithTag:442];
[cancelButton setTitle:@"Go Back" forState:UIControlStateNormal];
loading.text = @"Cannot find any players. Please try again later.";

} else if (match != nil) {

//save match
self.match = match;
self.match.delegate = self;

loading.text = @"Found a player. Preparing session...";

if (!self.matchStarted && match.expectedPlayerCount == 0) {

self.matchStarted = YES;
//begin game logic
[self.scene setState:1];
self.myTicket = 1000+arc4random_uniform(999);
[self.scene send:self.myTicket];
[self stopLoading];
}

}
}];
}

但是,当一台或多台设备通过蜂窝网络连接到互联网时,配对会失败。当我调查潜在的错误时,我发现即使是 wifi 到 wifi 的情况,完成处理程序也不会按预期工作。也就是说,match.expectedPlayerCount 永远不会为 0。相反,游戏在 - (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state 时开始 处理程序在完成处理程序之后调用,如下所示:

...
- (void)match:(GKMatch *)match player:(NSString *)playerID didChangeState:(GKPlayerConnectionState)state {
switch (state) {
case GKPlayerStateConnected:
self.matchStarted = YES;
//begin game logic
[self.scene setState:1];
self.myTicket = 1000+arc4random_uniform(999);
[self.scene send:self.myTicket];
[self stopLoading];
break;
...

现在的问题是,如果连接了 3g 的设备(并且匹配)didChangeState 永远不会被调用。我在 Internet 和本网站上检查了其他几个相关问题,尽管它们远非令人满意。我还读到 Game Center 的沙盒服务器不可靠,并且对于某些人来说生产版本工作完美(它只是工作!)尽管沙盒模式存在错误,但我不想冒这个风险。有人在玩多人游戏时遇到过类似问题吗?

最佳答案

哈格,

您的代码没有任何问题。您必须允许您的应用使用蜂窝数据,这需要用户许可。

以下段落选自Apple's support website :

At the Foundation layer, you can use the setAllowsCellularAccess: method on NSMutableURLRequest to specify whether a request can be sent over a cellular connection. You can also use the allowsCellularAccess to check the current value.

At the Core Foundation layer, you can achieve the same thing by setting the kCFStreamPropertyNoCellular property before opening a stream obtained from the CFSocketStream or CFHTTPStream APIs.

In older versions of iOS, you can continue to use the kSCNetworkReachabilityFlagsIsWWAN as a best-effort way of determining whether traffic will be sent over a cellular connection, but you should be aware of its limitations.

祝你好运

伊曼

关于ios - 3G 连接的 GameKit 配对失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29522921/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com