gpt4 book ai didi

objective-c - 如何跨设备共享保存的游戏?

转载 作者:太空狗 更新时间:2023-10-30 03:45:26 36 4
gpt4 key购买 nike

我在我的 iOS 游戏中实现了 GameKit,包括保存的游戏功能。

这是我如何保存和加载游戏的示例:

MobSvcSavedGameData.h

#ifndef MOBSVC_SAVEDGAMEDATA_H
#define MOBSVC_SAVEDGAMEDATA_H

#import <Foundation/Foundation.h>

@interface MobSvcSavedGameData : NSObject <NSCoding>

@property (readwrite, retain) NSString *data;

+(instancetype)sharedGameData;
-(void)reset;

@end


#endif /* MOBSVC_SAVEDGAMEDATA_H */

MobSvcSavedGameData.m

#import "MobSvcSavedGameData.h"
#import <Foundation/Foundation.h>

@interface MobSvcSavedGameData () <NSObject, NSCoding>

@end

@implementation MobSvcSavedGameData

#pragma mark MobSvcSavedGameData implementation

static NSString * const sgDataKey = @"data";

+ (instancetype)sharedGameData {
static id sharedInstance = nil;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});

return sharedInstance;
}

- (void)reset
{
self.data = nil;
}

- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:self.data forKey: sgDataKey];
}

- (nullable instancetype)initWithCoder:(nonnull NSCoder *)decoder {
self = [self init];
if (self) {
self.data = [decoder decodeObjectForKey:sgDataKey];
}
return self;
}

@end

为简单起见,我上面保存的游戏对象只有一个 NSString,它将像这样被序列化和上传:

void MobSvc::uploadSavedGameDataAwait(const char *name, const char *data)
{
GKLocalPlayer *mobSvcAccount = [GKLocalPlayer localPlayer];

if(mobSvcAccount.isAuthenticated)
{
MobSvcSavedGameData *savedGameData = [[MobSvcSavedGameData alloc] init];
savedGameData.data = [NSString stringWithUTF8String:data];
[mobSvcAccount saveGameData:[NSKeyedArchiver archivedDataWithRootObject:savedGameData] withName:[[NSString alloc] initWithUTF8String:name] completionHandler:^(GKSavedGame * _Nullable savedGame __unused, NSError * _Nullable error) {
if(error == nil)
{
NSLog(@"Successfully uploaded saved game data");
}
else
{
NSLog(@"Failed to upload saved game data: %@", error.description);
}
}];
}
}

这就是我在下一个游戏 session 中再次下载最近保存的游戏的方式:

void MobSvc::downloadSavedGameDataAwait(const char *name)
{
GKLocalPlayer *mobSvcAccount = [GKLocalPlayer localPlayer];

if(mobSvcAccount.isAuthenticated)
{
[mobSvcAccount fetchSavedGamesWithCompletionHandler:^(NSArray<GKSavedGame *> * _Nullable savedGames, NSError * _Nullable error) {
if(error == nil)
{
GKSavedGame *savedGameToLoad = nil;
for(GKSavedGame *savedGame in savedGames) {
const char *sname = savedGame.name.UTF8String;
if(std::strcmp(sname, name) == 0)
{
if (savedGameToLoad == nil || savedGameToLoad.modificationDate < savedGame.modificationDate) {
savedGameToLoad = savedGame;
}
}
}
if(savedGameToLoad != nil) {
[savedGameToLoad loadDataWithCompletionHandler:^(NSData * _Nullable data, NSError * _Nullable error) {
if(error == nil)
{
MobSvcSavedGameData *savedGameData = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(@"Successfully downloaded saved game data: %@", [savedGameData.data cStringUsingEncoding:NSUTF8StringEncoding]);
}
else
{
NSLog(@"Failed to download saved game data: %@", error.description);
}
}];
}
}
else
{
NSLog(@"Failed to prepare saved game data: %@", error.description);
}
}];
}
}

我通过上传一个随机字符串并在下一个 session 中使用相同的 name 接收它来测试它。有用!但是,一旦我尝试从我的第二部 iPhone 下载已保存的游戏,它就无法运行。在两部手机上,我都登录了同一个 Game-Center 帐户,我可以通过比较 GKLocalPlayer 实例中的 playerId 来确认这一点。

我已经设置了正确的 iCloud 容器并将我的游戏链接到它,但 iCloud 容器后端的日志仍然是空的。

这是怎么回事?如何在 Apple 设备之间共享保存的游戏?

最佳答案

问题中的上述示例工作得很好。用户登录 iCloud 并在 Game Center 登录时使用相同的苹果 ID 是强制性的,因为保存的游戏将存储在 iCloud 中。

不幸的是,我在没有 iCloud 的情况下进行整体测试,所以它无法工作。

关于objective-c - 如何跨设备共享保存的游戏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52975069/

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