on 09-29-2022 2:08 PM
Hi,
I'm trying to use SAPcpmsUserRoles to load user name, but experiencing that the return is nil.
I'm running this directly after the onboarding and therefore using the context of the sapURLsession.
My code is the following:
let settingsParameters = SAPcpmsSettingsParameters(backendURL: configurationURL, applicationID: appParameters["Application Identifier"] as? String)<br>var userRoles = SAPcpmsUserRoles(sapURLSession: context.sapURLSession, settingsParameters: settingsParameters)<br>userRoles.load { userInfo, error in<br> print(userInfo?.givenName ?? "NO DATA»)<br>}
When i try to print settingsParameters, it returns destinations: nil:
SAPcpmsSettingsParameters(applicationID: HIDDEN, applicationVersion: Optional("1.0"), backendURL: https://mobile-services-free-tier-HIDDEN, destinations: nil, deviceID: Optional("HIDDEN"))
If I try to get the user data through GET request (as described here) I get all the data I need.
Help is much appreciated.
I fetch the user name this way:
username = try? userRepository.fetchUsername()
import SAPOfflineOData
import SAPFoundation
import SAPCommon
protocol UserRepository {
func fetchUsername() throws -> String
}
class UserRepositoryImpl: UserRepository {
private static let usernameKey = "username"
private var sapUrlSession: SAPURLSession
public enum UserRepositoryException: Error {
case UserNameIsNotAvailableException
}
init(sapUrlSession: SAPURLSession){
self.sapUrlSession = sapUrlSession
}
func fetchUsername() throws -> String {
let userRoles = createUserRolesAccessInstance()
var roleInfo: SAPcpmsUserRoles.SAPcpmsUserInfo?
var error: Error?
let dispatchGroup = DispatchGroup()
dispatchGroup.enter()
userRoles.load() { roleInfoResult, errorResult in
roleInfo = roleInfoResult
error = errorResult
dispatchGroup.leave()
}
dispatchGroup.wait()
return try handleUserRoleLoadingResult(roleInfo: roleInfo, error: error)
}
private func createUserRolesAccessInstance() -> SAPcpmsUserRoles {
return SAPcpmsUserRoles(sapURLSession: sapUrlSession, settingsParameters: AppDelegate.shared.sessionManager.onboardingSession!.settingsParameters!)
}
private func handleUserRoleLoadingResult(roleInfo: SAPcpmsUserRoles.SAPcpmsUserInfo?, error: Error?) throws -> String {
if let error = error {
if let username = tryToGetUserNameFromCache() {
return username
}
throw error
}
if let name = roleInfo?.userName {
cacheUserName(userName: name)
return name
}
else {
throw UserRepositoryException.UserNameIsNotAvailableException
}
}
private func cacheUserName(userName: String) {
UserDefaults.standard.set(userName, forKey: UserRepositoryImpl.usernameKey)
}
private func tryToGetUserNameFromCache() -> String? {
return UserDefaults.standard.value(forKey: UserRepositoryImpl.usernameKey) as? String
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
90 | |
10 | |
10 | |
10 | |
7 | |
7 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.