Skip to content

Commit 5d8c4fb

Browse files
authored
Merge pull request #641 from Iterable/omni-cg-master
SetEmail SetUserID Callbacks MOB - 6055
2 parents 4f684ab + 61d635e commit 5d8c4fb

File tree

3 files changed

+97
-8
lines changed

3 files changed

+97
-8
lines changed

swift-sdk/Internal/InternalIterableAPI.swift

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
111111
_payloadData = data
112112
}
113113

114-
func setEmail(_ email: String?, authToken: String? = nil) {
114+
func setEmail(_ email: String?, authToken: String? = nil, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil) {
115115
ITBInfo()
116116

117117
if _email == email && email != nil && authToken != nil {
@@ -127,13 +127,15 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
127127

128128
_email = email
129129
_userId = nil
130+
_successCallback = successHandler
131+
_failureCallback = failureHandler
130132

131133
storeIdentifierData()
132134

133135
onLogin(authToken)
134136
}
135137

136-
func setUserId(_ userId: String?, authToken: String? = nil) {
138+
func setUserId(_ userId: String?, authToken: String? = nil, successHandler: OnSuccessHandler? = nil, failureHandler: OnFailureHandler? = nil) {
137139
ITBInfo()
138140

139141
if _userId == userId && userId != nil && authToken != nil {
@@ -149,6 +151,8 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
149151

150152
_email = nil
151153
_userId = userId
154+
_successCallback = successHandler
155+
_failureCallback = failureHandler
152156

153157
storeIdentifierData()
154158

@@ -167,6 +171,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
167171
guard let appName = pushIntegrationName else {
168172
let errorMessage = "Not registering device token - appName must not be nil"
169173
ITBError(errorMessage)
174+
_failureCallback?(errorMessage, nil)
170175
onFailure?(errorMessage, nil)
171176
return
172177
}
@@ -181,8 +186,15 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
181186
sdkVersion: localStorage.sdkVersion)
182187
requestHandler.register(registerTokenInfo: registerTokenInfo,
183188
notificationStateProvider: notificationStateProvider,
184-
onSuccess: onSuccess,
185-
onFailure: onFailure)
189+
onSuccess: { (_ data: [AnyHashable: Any]?) in
190+
self._successCallback?(data)
191+
onSuccess?(data)
192+
},
193+
onFailure: { (_ reason: String?, _ data: Data?) in
194+
self._failureCallback?(reason, data)
195+
onFailure?(reason, data)
196+
}
197+
)
186198
}
187199

188200
@discardableResult
@@ -428,6 +440,9 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
428440
private var _email: String?
429441
private var _payloadData: [AnyHashable: Any]?
430442
private var _userId: String?
443+
private var _successCallback: OnSuccessHandler? = nil
444+
private var _failureCallback: OnFailureHandler? = nil
445+
431446

432447
/// the hex representation of this device token
433448
private var hexToken: String?
@@ -537,6 +552,8 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
537552

538553
if config.autoPushRegistration {
539554
notificationStateProvider.registerForRemoteNotifications()
555+
} else {
556+
_successCallback?([:])
540557
}
541558

542559
_ = inAppManager.scheduleSync()

swift-sdk/IterableAPI.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ import UIKit
129129

130130
// MARK: - SDK
131131

132-
public static func setEmail(_ email: String?, _ authToken: String? = nil) {
133-
implementation?.setEmail(email, authToken: authToken)
132+
public static func setEmail(_ email: String?, _ authToken: String? = nil, _ successHandler: OnSuccessHandler? = nil, _ failureHandler: OnFailureHandler? = nil) {
133+
implementation?.setEmail(email, authToken: authToken, successHandler: successHandler, failureHandler: failureHandler)
134134
}
135135

136-
public static func setUserId(_ userId: String?, _ authToken: String? = nil) {
137-
implementation?.setUserId(userId, authToken: authToken)
136+
public static func setUserId(_ userId: String?, _ authToken: String? = nil, _ successHandler: OnSuccessHandler? = nil, _ failureHandler: OnFailureHandler? = nil) {
137+
implementation?.setUserId(userId, authToken: authToken, successHandler: successHandler, failureHandler: failureHandler)
138138
}
139139

140140
/// Handle a Universal Link

tests/unit-tests/IterableAPITests.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,78 @@ class IterableAPITests: XCTestCase {
165165

166166
wait(for: [expectation], timeout: testExpectationTimeout)
167167
}
168+
169+
func testSetEmailWithCallbackSuccess() {
170+
let expectation = XCTestExpectation(description: "Set email with callback success")
171+
172+
let config = IterableConfig()
173+
let networkSession = MockNetworkSession(statusCode: 200)
174+
let internalAPI = InternalIterableAPI.initializeForTesting(apiKey: IterableAPITests.apiKey, config: config, networkSession: networkSession)
175+
176+
internalAPI.setEmail("[email protected]", successHandler: { success in
177+
XCTAssertNotNil(success)
178+
expectation.fulfill()
179+
}, failureHandler: { _, _ in
180+
XCTFail("Failed to set email")
181+
expectation.fulfill()
182+
})
183+
internalAPI.register(token: "zeeToken".data(using: .utf8)!)
184+
wait(for: [expectation], timeout: testExpectationTimeout)
185+
}
186+
187+
func testSetEmailWithCallbackFailure() {
188+
let expectation = XCTestExpectation(description: "Set email with callback failure")
189+
190+
let config = IterableConfig()
191+
let networkSession = MockNetworkSession(statusCode: 400)
192+
let internalAPI = InternalIterableAPI.initializeForTesting(apiKey: IterableAPITests.apiKey, config: config, networkSession: networkSession)
193+
194+
internalAPI.setEmail("invalid_email", successHandler: { success in
195+
XCTFail("Email should not be set successfully")
196+
expectation.fulfill()
197+
}, failureHandler: { _, error in
198+
XCTAssertNotNil(error)
199+
expectation.fulfill()
200+
})
201+
internalAPI.register(token: "zeeToken".data(using: .utf8)!)
202+
wait(for: [expectation], timeout: testExpectationTimeout)
203+
}
204+
205+
func testSetUserIdWithCallbackSuccess() {
206+
let expectation = XCTestExpectation(description: "Set user ID with callback success")
207+
208+
let config = IterableConfig()
209+
let networkSession = MockNetworkSession(statusCode: 200)
210+
let internalAPI = InternalIterableAPI.initializeForTesting(apiKey: IterableAPITests.apiKey, config: config, networkSession: networkSession)
211+
212+
internalAPI.setUserId("user123", successHandler: { success in
213+
XCTAssertNotNil(success)
214+
expectation.fulfill()
215+
}, failureHandler: { _, _ in
216+
XCTFail("Failed to set user ID")
217+
expectation.fulfill()
218+
})
219+
internalAPI.register(token: "zeeToken".data(using: .utf8)!)
220+
wait(for: [expectation], timeout: testExpectationTimeout)
221+
}
222+
223+
func testSetUserIdWithCallbackFailure() {
224+
let expectation = XCTestExpectation(description: "Set user ID with callback failure")
225+
226+
let config = IterableConfig()
227+
let networkSession = MockNetworkSession(statusCode: 400)
228+
let internalAPI = InternalIterableAPI.initializeForTesting(apiKey: IterableAPITests.apiKey, config: config, networkSession: networkSession)
229+
230+
internalAPI.setUserId("user123", successHandler: { success in
231+
XCTFail("User ID should not be set successfully")
232+
expectation.fulfill()
233+
}, failureHandler: { _, error in
234+
XCTAssertNotNil(error)
235+
expectation.fulfill()
236+
})
237+
internalAPI.register(token: "zeeToken".data(using: .utf8)!)
238+
wait(for: [expectation], timeout: testExpectationTimeout)
239+
}
168240

169241
func testEmailPersistence() {
170242
let internalAPI = InternalIterableAPI.initializeForTesting()

0 commit comments

Comments
 (0)