Skip to content

onGetUserInfo:dataJson

获取用户基本信息

该方法用于获取原APP用户信息。主要用途是:为游戏提供APP非敏感用户信息。


方法声明

objectivec
/**
 * 当 SDK 请求获取用户基本信息(User Info)时触发。
 * App 应根据此请求通过 stateHandle 返回用户的昵称、头像等基础信息。
 *
 * @param stateHandle 用于向 SDK 返回处理结果的状态句柄。
 * @param dataJson 包含请求参数(如用户 ID 列表)的 JSON 字符串。
 */
- (void)onGetUserInfo:(id<SUDOPStateHandle>)stateHandle dataJson:(NSString*)dataJson;

运行行为

  • 触发时机:通常发生在游戏初始化完成、新玩家加入房间、或用户在游戏内触发“查看他人资料”时。
  • 标准 Key 定义:App 必须使用 SUDOP 协议约定的字段(如 nickname, avatar)构造返回的 JSON。
  • 异步闭环:由于用户信息可能需要从 App Server 获取,App 可以在网络请求的回调中通过 [stateHandle success:] 异步同步数据。

代码示例

实现基础信息的解析、组装与回传:

objectivec
- (void)onGetUserInfo:(id<SUDOPStateHandle>)stateHandle dataJson:(NSString*)dataJson {
    
    NSDictionary *result = @{
            @"userId": uid,
            @"nickname": [NSString stringWithFormat:@"iOS用户_%@", uid],
            @"avatar": @"https://cdn.example.com/avatars/default.png"
        };
    
    // 4. 将结果转为 JSON 字符串并回传
    NSData *resultData = [NSJSONSerialization dataWithJSONObject:result options:0 error:nil];
    NSString *resultJson = [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];
    
    [stateHandle success:resultJson];
}

注意事项

  • 头像规格:建议提供高清晰度且比例为 1:1 的头像 URL,以确保在不同尺寸的游戏 UI 中显示效果统一。
  • 缓存优化:对于频繁请求的用户信息,建议 App 侧建立二级缓存,避免由于网络延迟导致游戏内玩家头像出现长时间的占位图。
  • 字段严谨性:务必确认返回的 JSON 字段名与协议定义一致(通常在 SUDOP 常量类中定义),否则游戏引擎可能无法识别数据。