iOS SDK V2 Authentication
| Ngôn ngữ | API sử dụng | Trường hợp |
|---|---|---|
| Objective-C | OEGManager | Egret, Cocos, game template ObjC |
| Swift | OEGAuth | Game viết thuần Swift |
OEGAuth là Swift-only — không visible từ Objective-C. Game sử dụng Egret/Cocos template (file .mm) phải dùng OEGManager.
Objective-C — OEGManager
Đây là API dành cho hầu hết game templates (Egret, Cocos Creator, v.v.).
Khởi động SDK
@import OegSdkV2;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[OEGManager handleDidFinishLaunchingWithOptions:launchOptions];
// ...
return YES;
}
Hiển thị màn hình đăng nhập
[[OEGManager sharedManager] showLoginWithCallback:^(OEGActionType type, id response, BOOL success, NSError *error) {
if (success) {
NSString *token = [OEGManager getAPIToken];
NSString *uuid = [OEGManager getAPIUUID];
// Gửi token về game client
}
}];
Lấy thông tin user
// Toàn bộ user info dưới dạng NSDictionary
NSDictionary *user = [OEGManager getCurrentUser]; // nil nếu chưa đăng nhập
NSString *userId = user[@"userId"]; // numeric OEG user ID
NSString *uuid = user[@"uuid"]; // UUID ổn định qua các session
NSString *token = user[@"token"]; // JWT bearer token
NSString *username = user[@"username"]; // nil với guest account
NSString *displayName = user[@"displayName"];
NSString *email = user[@"email"];
NSString *phone = user[@"phone"];
BOOL isGuest = [user[@"isGuest"] boolValue]; // YES với Play Now / guest
// Getters riêng lẻ
NSString *token = [OEGManager getAPIToken]; // JWT token
NSString *uuid = [OEGManager getAPIUUID]; // UUID
NSString *userId = [OEGManager getUserId]; // numeric user ID
BOOL isGuest = [OEGManager isPlayNow]; // guest account check
Device info
NSDictionary *device = [OEGManager getDeviceInfo];
NSNumber *osId = device[@"osId"]; // 2 = iOS
NSString *deviceId = device[@"deviceId"]; // persistent device identifier
NSString *sdkId = device[@"sdkId"];
NSString *packageName = device[@"packageName"];
NSString *gameVersion = device[@"gameVersion"];
NSNumber *gameId = device[@"gameId"];
Logout
[[OEGManager sharedManager] logout];
Bắt sự kiện Logout
Trong các game template dùng OEGManager, callback được truyền vào lúc gọi showLoginWithCallback: sẽ tự động được gọi lại với type = 2 (tương ứng với OEGActionTypeLogout) bất cứ khi nào user bị đăng xuất (do chủ động bấm đăng xuất từ SDK, tài khoản bị khoá, hoặc lỗi token).
[[OEGManager sharedManager] showLoginWithCallback:^(OEGActionType type, id response, BOOL success, NSError *error) {
if (type == 2) {
// User đã đăng xuất khỏi SDK.
// Game nên thực hiện logic văng ra màn hình đăng nhập.
NSLog(@"User logged out!");
} else if (success) {
// Đăng nhập thành công
}
}];
Hiển thị Profile / Dashboard
Objective-C games dùng showProfileWithCallback: để mở màn hình profile/dashboard của SDK:
[[OEGManager sharedManager] showProfileWithCallback:^(OEGActionType type, id response, BOOL success, NSError *error) {
if (type == OEGActionTypeLogout) {
// User đã đăng xuất từ dashboard — navigate về màn hình đăng nhập của game
}
}];
Set Game Role
Gọi sau khi player vào game và chọn server/character:
[OEGSdkCore setGameRoleWithServerId:@"server_01"
serverName:@"Server 1"
roleId:@"character_123"
roleName:@"DragonSlayer"
level:50];
Lifecycle
- (void)applicationWillResignActive:(UIApplication *)application {
[OEGManager handleWillResignActive];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
[OEGManager handleDidEnterBackground];
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
[OEGManager handleWillEnterForeground];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[OEGManager handleDidBecomeActive];
}
- (void)applicationWillTerminate:(UIApplication *)application {
[OEGManager handleWillTerminate];
}
Swift — OEGAuth
Dành cho game viết thuần Swift.
Login
import OegSdkV2
OEGAuth.shared.login(username: "user", password: "pass") { result in
switch result {
case .success(let user):
print("Logged in: \(user.userId)")
case .error(_, let message):
print("Login failed: \(message)")
}
}
Guest login:
OEGAuth.shared.playNow { result in
// handle AuthResult
}
Logout:
OEGAuth.shared.logout()
Bắt sự kiện Logout
SDK sẽ tự động phát ra một Notification mỗi khi user bị đăng xuất (do chủ động bấm Đăng xuất từ Dashboard, bị lỗi token, v.v.). Game nên lắng nghe sự kiện này để văng ra màn hình đăng nhập chính của game.
NotificationCenter.default.addObserver(
forName: Notification.Name("OEGUserDidLogout"),
object: nil,
queue: .main
) { notification in
// Thực hiện logic văng ra màn hình đăng nhập của game
print("User đã logout khỏi SDK!")
}
(Lưu ý: Nếu game sử dụng OEGManager.sharedManager.showLogin thay vì dùng trực tiếp OEGAuth trong Swift, callback cũng sẽ nhận được sự kiện với tham số type == .logout tương tự như ví dụ ở phần Objective-C).
Lấy thông tin user
let user = OEGAuth.shared.currentUser // AuthUser? — nil nếu chưa đăng nhập
let userId = user?.userId // String — numeric OEG user ID
let uuid = user?.uuid // String — UUID ổn định qua các session
let username = user?.username // String? — nil với guest account
let token = user?.token // String — JWT bearer token
let loginType = user?.loginType // LoginType enum
let isGuest = user?.isPlayNow // true với Play Now / guest
// Profile
let displayName = user?.displayName
let email = user?.email
let phone = user?.phone
let avatar = user?.avatar
LoginType
switch user?.loginType {
case .password: // username + password
case .playNow: // anonymous guest
case .google: // Google Sign-In
case .facebook: // Facebook Login
case .tiktok: // TikTok Login
case .apple: // Apple Sign-In
case .unknown: // restored from storage — không có fresh login session này
case nil: // chưa đăng nhập
}
Session state
let isLoggedIn = OEGAuth.shared.isLoggedIn
let token = OEGAuth.shared.token
let uuid = OEGAuth.shared.userUUID
Device info
let device = OEGAuth.shared.getDeviceInfo()
let osId = device.osId // 2 = iOS
let deviceId = device.deviceId // persistent device identifier
let sdkId = device.sdkId
let packageName = device.packageName
let gameVersion = device.gameVersion
let gameId = device.gameId
Set Game Role
OegSdkCore.setGameRole(
serverId: "server_01",
serverName: "Server 1", // optional
roleId: "character_123",
roleName: "DragonSlayer", // optional
level: 50 // optional
)
Social login
Apple
Xử lý tự động qua built-in login UI — không cần config thêm ngoài oeg_config.json.
Google / Facebook
Cần cấu hình credentials trong oeg_config.json:
{
"sns": {
"google_web_client_id": "your-web-client-id.apps.googleusercontent.com",
"facebook_app_id": "your-facebook-app-id",
"facebook_client_token": "your-facebook-client-token"
}
}
Wiring URL callback trong AppDelegate:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
return [OEGManager handleOpenURL:url options:options];
}
TikTok
See the dedicated TikTok Login guide for full setup instructions including TikTok Developer Portal configuration, SPM dependency, Info.plist setup, URL callback wiring, and how the auth flow works.
Real-time Maintenance Guard
SDK tự động kiểm tra lịch bảo trì — không cần tích hợp thêm:
- Login Block: Nếu đang bảo trì, chặn login và hiển thị alert.
- In-Game Countdown: Hiển thị banner đếm ngược không làm gián đoạn gameplay.
- Auto Kick Out: Khi đếm ngược xong, SDK logout và hiển thị popup bảo trì.
- Whitelist: Account được đánh dấu tester trong CMS sẽ bypass toàn bộ maintenance block.
Cấu hình nâng cao (Swift)
OegSdkCore.Builder()
.setMaintainPollingInterval(60) // giây, mặc định 60, tối thiểu 30, 0 = tắt
.onMaintenanceKickOut { info in
// Gọi trước khi hiển thị popup — dùng để lưu game state
}
// ...
.build()