diff --git a/packages/ios/Demo/Demo/SetupView.swift b/packages/ios/Demo/Demo/SetupView.swift index e2eac11b4f..6a158b0734 100644 --- a/packages/ios/Demo/Demo/SetupView.swift +++ b/packages/ios/Demo/Demo/SetupView.swift @@ -17,10 +17,10 @@ struct SetupView: View { let config = FormbricksConfig.Builder(appUrl: "[appUrl]", environmentId: "[environmentId]") .setLogLevel(.debug) .build() + // Simulate async setup delay DispatchQueue.global().async { Formbricks.setup(with: config) - Formbricks.setUserId(UUID().uuidString) DispatchQueue.main.async { isSetup = true @@ -31,6 +31,10 @@ struct SetupView: View { .padding() } } else { + Button("Call Formbricks.setUserId with a random userId") { + Formbricks.setUserId(UUID().uuidString) + }.padding() + Button("Call Formbricks.track") { Formbricks.track("click_demo_button") } diff --git a/packages/ios/FormbricksSDK/FormbricksSDK/Formbricks.swift b/packages/ios/FormbricksSDK/FormbricksSDK/Formbricks.swift index c5cca4bdd3..26ab43931f 100644 --- a/packages/ios/FormbricksSDK/FormbricksSDK/Formbricks.swift +++ b/packages/ios/FormbricksSDK/FormbricksSDK/Formbricks.swift @@ -58,7 +58,7 @@ import Network if let userId = config.userId { userManager?.set(userId: userId) } - if let attributes = config.attributes { + if let attributes = config.attributes, !attributes.isEmpty { userManager?.set(attributes: attributes) } if let language = config.attributes?["language"] { diff --git a/packages/ios/FormbricksSDK/FormbricksSDK/Manager/UserManager.swift b/packages/ios/FormbricksSDK/FormbricksSDK/Manager/UserManager.swift index 08f0a344b5..dccea0ac59 100644 --- a/packages/ios/FormbricksSDK/FormbricksSDK/Manager/UserManager.swift +++ b/packages/ios/FormbricksSDK/FormbricksSDK/Manager/UserManager.swift @@ -106,6 +106,14 @@ final class UserManager: UserManagerSyncable { /// Logs out the user and clears the user state. func logout() { + var isUserIdDefined = false + + if userId != nil { + isUserIdDefined = true + } else { + Formbricks.logger?.error("no userId is set, please set a userId first using the setUserId function") + } + UserDefaults.standard.removeObject(forKey: UserManager.userIdKey) UserDefaults.standard.removeObject(forKey: UserManager.contactIdKey) UserDefaults.standard.removeObject(forKey: UserManager.segmentsKey) @@ -122,7 +130,10 @@ final class UserManager: UserManagerSyncable { backingExpiresAt = nil updateQueue?.reset() - Formbricks.logger?.debug("Successfully logged out user and reset the user state.") + if isUserIdDefined { + Formbricks.logger?.debug("Successfully logged out user and reset the user state.") + } + } func cleanupUpdateQueue() {