Skip to content

Instantly share code, notes, and snippets.

@magicien
Last active May 30, 2020 13:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magicien/c05a267d88030bf49100186ead070487 to your computer and use it in GitHub Desktop.
Save magicien/c05a267d88030bf49100186ead070487 to your computer and use it in GitHub Desktop.
Epic Online Services: Get user data sample in Swift
import EOSSDK
var platformHandle: EOS_HPlatform
// 1. Initialize
// https://gist.github.com/magicien/a892304c22574d59923dccc95571b990
func init() {
// Initialize App, Platform
// Login
// EOS_Auth_Login(authHandle, &options, nil, loginFunc)
}
// 2. Login callback: Get a logged-in user ID (LocalUserId)
func loginFunc(data: Optional<UnsafePointer<_tagEOS_Auth_LoginCallbackInfo>>) {
guard let info = data?.pointee else { return }
guard let authHandle = EOS_Platform_GetAuthInterface(platformHandle) else { return }
if info.ResultCode == EOS_Success {
let accountCount = EOS_Auth_GetLoggedInAccountsCount(authHandle)
for i in 0..<accountCount {
let accountId = EOS_Auth_GetLoggedInAccountByIndex(authHandle, i)
let loginStatus = EOS_Auth_GetLoginStatus(authHandle, accountId)
// Do something
}
getPlayerInfo(accountID: info.LocalUserId)
} else if info.ResultCode == EOS_OperationWillRetry {
Swift.print("retrying...")
} else if info.ResultCode == EOS_Auth_PinGrantCode {
Swift.print("Waiting for PIN")
} else if info.ResultCode == EOS_Auth_MFARequired {
Swift.print("Needs MFA")
} else {
if let errorString = EOS_EResult_ToString(info.ResultCode) {
let str = String(cString: errorString)
Swift.print("Login failed: \(str)")
} else {
Swift.print("Login failed")
}
}
}
// 3. Send a query to get the user data
func getPlayerInfo(accountID: EOS_EpicAccountId) {
guard let userInfo = EOS_Platform_GetUserInfoInterface(platformHandle) else {
return
}
var options = EOS_UserInfo_QueryUserInfoOptions()
options.ApiVersion = EOS_USERINFO_QUERYUSERINFO_API_LATEST
options.LocalUserId = accountID
options.TargetUserId = accountID
EOS_UserInfo_QueryUserInfo(userInfo, &options, nil, queryUserInfoFunc)
}
// 4. Do something with the user data
func queryUserInfoFunc(data: Optional<UnsafePointer<_tagEOS_UserInfo_QueryUserInfoCallbackInfo>>) {
guard let info = data?.pointee else {
return
}
let buffer = UnsafeMutablePointer<Int8>.allocate(capacity: Int(EOS_LOCALECODE_MAX_BUFFER_LEN))
var len = EOS_LOCALECODE_MAX_BUFFER_LEN
// User locale
let result = EOS_Platform_GetActiveLocaleCode(platformHandle, info.LocalUserId, buffer, &len)
guard result == EOS_Success else {
Swift.print("Error: Failed to get locale code")
return
}
// "en" for English
let locale = String(cString: buffer)
buffer.deallocate()
var options = EOS_UserInfo_CopyUserInfoOptions()
options.ApiVersion = EOS_USERINFO_COPYUSERINFO_API_LATEST
options.LocalUserId = info.LocalUserId
options.TargetUserId = info.TargetUserId
let interface = EOS_Platform_GetUserInfoInterface(platformHandle!)
var userInfo: UnsafeMutablePointer<EOS_UserInfo>? = nil
let userResult = EOS_UserInfo_CopyUserInfo(interface, &options, &userInfo)
guard let user = userInfo?.pointee else { return }
Swift.print("Nickname: \(user.Nickname)")
// ApiVersion, Country, DisplayName, Nickname, PreferredLanguage, UserId
EOS_UserInfo_Release(userInfo)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment