SDK 集成
AppDelegate 配置
AppDelegate.h — 导入 SDK:
#import <VGPSDK/VGPSDK.h>
AppDelegate.m — 注册事件监听并转发生命周期调用:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// 注册 VGP SDK 事件监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LoginSuccess:) name:VGP_EVENT_LOGIN_SUCCESS object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(LogoutSuccess:) name:VGP_EVENT_LOGOUT_SUCCESS object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(SDKReady) name:VGP_EVENT_INIT_READY object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(PurchaseSuccess) name:VGP_EVENT_PURCHASE_SUCCESS object:nil];
return [[VGPInterface sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[[VGPInterface sharedInstance] applicationDidBecomeActive:application];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
return [[VGPInterface sharedInstance] application:application openURL:url options:options];
}
// Universal Links
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
return [[VGPInterface sharedInstance] application:application continueUserActivity:userActivity restorationHandler:restorationHandler];
}
// 推送通知数据
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[[VGPInterface sharedInstance] application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
// 推送 Token 注册
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
[[VGPInterface sharedInstance] application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
[[VGPInterface sharedInstance] application:application didFailToRegisterForRemoteNotificationsWithError:error];
}
SceneDelegate(iOS 13+)
SceneDelegate.h:
#import <VGPSDK/VGPSDK.h>
SceneDelegate.m:
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions API_AVAILABLE(ios(13.0)) {
[[VGPInterface sharedInstance] scene:scene willConnectToSession:session options:connectionOptions];
}
- (void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts API_AVAILABLE(ios(13.0)) {
[[VGPInterface sharedInstance] scene:scene openURLContexts:URLContexts];
}
- (void)scene:(UIScene *)scene continueUserActivity:(NSUserActivity *)userActivity API_AVAILABLE(ios(13.0)) {
[[VGPInterface sharedInstance] scene:scene continueUserActivity:userActivity];
}
- (void)sceneDidBecomeActive:(UIScene *)scene API_AVAILABLE(ios(13.0)) {
[[VGPInterface sharedInstance] sceneDidBecomeActive:scene];
}
登录 / 登出回调
实现上面注册的事件处理方法:
- (void)LoginSuccess:(NSNotification *)notification {
NSString *vgpID = notification.userInfo[@"id"];
NSString *vgpToken = notification.userInfo[@"token"];
NSLog(@"登录成功 — ID: %@, token: %@", vgpID, vgpToken);
// TODO: 将 vgpID 和 vgpToken 发送给您的游戏服务器
}
- (void)LogoutSuccess:(NSNotification *)notification {
NSLog(@"登出成功");
// TODO: 返回登录界面
}
- (void)PurchaseSuccess {
NSLog(@"购买成功");
// TODO: 发放游戏内道具
}
从界面触发登录 / 登出:
- (IBAction)loginClick:(UIButton *)sender {
[[VGPInterface sharedInstance] loginGame];
}
- (IBAction)logoutClick:(UIButton *)sender {
[[VGPInterface sharedInstance] logoutGame];
}
等待 SDK 初始化完成
在网络较慢时,用户可能在 SDK 初始化完成前触发登录。请使用 VGP_EVENT_INIT_READY 延迟处理:
- (void)SDKReady {
// SDK 已完全初始化 — 可安全显示登录界面
[[VGPInterface sharedInstance] loginGame];
}
该监听器已在上面的 didFinishLaunchingWithOptions: 中注册。
支付
当用户选择商品时调用 purchase:。请向服务器团队获取 roleId、serverId 和 itemId。
[[VGPInterface sharedInstance] purchase:@"ROLE_ID"
serverID:@"SERVER_ID"
itemID:@"ITEM_ID"
andIAPData:@{}]; // 可选的附加元数据

数据分析事件
SDK 会将事件上报至 Firebase、Facebook、Adjust 和 TikTok。请在对应游戏操作后调用:
| 事件 | 调用时机 |
|---|---|
logCreatedCharacter | 创建新角色后 |
logJoinServer:setCharacterID: | 玩家选择服务器和角色后 |
logLevelUp: | 角色升级后 |
logPurchase:setItemID: | 购买成功后(传入 VND 收入分成金额) |
// 创建角色
[[VGPInterface sharedInstance] logCreatedCharacter];
// 加入服务器
[[VGPInterface sharedInstance] logJoinServer:@"server_1" setCharacterID:@"char_abc"];
// 角色升级(传入当前等级)
[[VGPInterface sharedInstance] logLevelUp:100];
// 购买上报(VND 收入分成,物品 ID 来自充值表)
// 示例:用户购买 85 xu ≈ 170 Vxu ≈ 17,000 VND
[[VGPInterface sharedInstance] logPurchase:17000 setItemID:@"gold.xxx.17000"];
收入分成参考:

logPurchase:setItemID:必须在服务器确认交易成功后才能调用。