iOS端快速接入
说明:本指南介绍了 SUDGI SDK 的极速集成方式。通过 authWithUserSignature 鉴权后,直接使用 startGameWithGameID 即可拉起并运行游戏。
一、 引入 SDK 与工程配置
1. 引入 SDK
- CocoaPods:在
Podfile中添加pod 'SUDGI', :path => '../../',执行pod install。 - 手动引入:将
SUDGI.xcframework拖入工程,在General中设置为 Embed & Sign。
2. 环境设置
- Other Linker Flags:添加
-ObjC。 - Enable Bitcode:设置为 NO。
- 引入头文件:
#import <SUDGI/SUDGI-umbrella.h>。
二、 SDK 初始化
在 App 启动时执行一次。
objc
SUDOPSDKConfiguration *config = [[SUDOPSDKConfiguration alloc] init];
config.appID = @"您的 AppID";
config.appKey = @"您的 AppKey";
[SUDOP initializeWithConfiguration:config completion:^(NSError * _Nullable error) {
if (!error) NSLog(@"SDK 初始化成功");
}];三、 用户鉴权 (auth)
在启动游戏前,必须将业务后端生成的 userSignature 告知 SDK。
objc
// 1. userSignature 由您的业务服务器生成并下发
NSString *userSignature = @"业务后端下发的签名字符串";
// 2. 建立用户合法会话
[SUDOP authWithUserSignature:userSignature completion:^(NSError * _Nullable error) {
if (!error) {
NSLog(@"鉴权成功");
}
}];四、 极速启动游戏
直接传入 gameID,SDK 将自动处理资源下载、校验和渲染。
objc
[SUDOP startGameWithGameID:@"game_101"
onCreated:^(id<SUDRTGameHandle> gameHandle) {
// 保存句柄用于后续销毁
self.gameHandle = gameHandle;
// 1. 注册代理以接收游戏事件
[SUDOP registerWrappedClientWithGameHandle:gameHandle clientDelegate:self];
// 2. 挂载视图到 UI
UIView *gameView = [gameHandle getGameView];
gameView.frame = self.view.bounds;
[self.view addSubview:gameView];
}
completion:^(id<SUDOPGameHandleProvider> provider, NSError *error) {
if (!error) NSLog(@"游戏已成功运行");
}];五、 销毁与资源释放
当用户退出游戏页面时,直接通过句柄销毁实例。
objc
if (self.gameHandle) {
[self.gameHandle destroy];
self.gameHandle = nil;
NSLog(@"游戏实例已销毁,内存已释放");
}