Skip to content

Commit 72cd8e4

Browse files
committed
🔧 Added sdk initialization checks to all API calls that warrant it. Code cleanup
1 parent 73e7a92 commit 72cd8e4

File tree

2 files changed

+121
-47
lines changed

2 files changed

+121
-47
lines changed

‎swift-sdk/Internal/InternalIterableAPI.swift

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
250250
func register(token: Data,
251251
onSuccess: OnSuccessHandler? = nil,
252252
onFailure: OnFailureHandler? = nil) {
253+
253254
guard let appName = pushIntegrationName else {
254255
let errorMessage = "Not registering device token - appName must not be nil"
255256
ITBError(errorMessage)
@@ -566,7 +567,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
566567
source: InAppDeleteSource? = nil,
567568
inboxSessionId: String? = nil,
568569
onSuccess: OnSuccessHandler? = nil,
569-
onFailure: OnFailureHandler? = nil) -> Pending<SendRequestValue, SendRequestError> {
570+
onFailure: OnFailureHandler? = nil) -> Pending<SendRequestValue, SendRequestError> {
570571
requestHandler.inAppConsume(message: message,
571572
location: location,
572573
source: source,
@@ -691,6 +692,17 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
691692
}
692693
}
693694

695+
696+
func isSDKInitialized() -> Bool {
697+
let isInitialized = !apiKey.isEmpty && isEitherUserIdOrEmailSet()
698+
699+
if !isInitialized {
700+
ITBInfo("Iterable SDK must be initialized with an API key and user email/userId before calling SDK methods")
701+
}
702+
703+
return isInitialized
704+
}
705+
694706
public func isEitherUserIdOrEmailSet() -> Bool {
695707
IterableUtil.isNotNullOrEmpty(string: _email) || IterableUtil.isNotNullOrEmpty(string: _userId)
696708
}
@@ -702,9 +714,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
702714
private func logoutPreviousUser() {
703715
ITBInfo()
704716

705-
guard isEitherUserIdOrEmailSet() else {
706-
return
707-
}
717+
guard isSDKInitialized() else { return }
708718

709719
if config.autoPushRegistration {
710720
disableDeviceForCurrentUser()
@@ -729,10 +739,12 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
729739
}
730740

731741
private func onLogin(_ authToken: String? = nil) {
742+
guard isSDKInitialized() else { return }
743+
732744
ITBInfo()
733745

734746
self.authManager.pauseAuthRetries(false)
735-
if let authToken = authToken {
747+
if let authToken {
736748
self.authManager.setNewToken(authToken)
737749
completeUserLogin()
738750
} else if isEitherUserIdOrEmailSet() && config.authDelegate != nil {
@@ -755,9 +767,7 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
755767
private func completeUserLogin() {
756768
ITBInfo()
757769

758-
guard isEitherUserIdOrEmailSet() else {
759-
return
760-
}
770+
guard isSDKInitialized() else { return }
761771

762772
if config.autoPushRegistration {
763773
notificationStateProvider.registerForRemoteNotifications()

‎swift-sdk/IterableAPI.swift

Lines changed: 103 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,10 @@ import UIKit
126126
callback?(false)
127127
}
128128

129-
if let _implementation = implementation, config.enableAnonTracking, !_implementation.isEitherUserIdOrEmailSet(), _implementation.getAnonymousUsageTracked(){
129+
if let implementation, config.enableAnonTracking, !implementation.isSDKInitialized(), implementation.getAnonymousUsageTracked() {
130130
ITBInfo("AUT ENABLED AND CONSENT GIVEN - Criteria fetched")
131-
_implementation.anonymousUserManager.getAnonCriteria()
132-
_implementation.anonymousUserManager.updateAnonSession()
131+
implementation.anonymousUserManager.getAnonCriteria()
132+
implementation.anonymousUserManager.updateAnonSession()
133133
}
134134
}
135135

@@ -250,7 +250,7 @@ import UIKit
250250
/// - SeeAlso: IterableConfig
251251
@objc(registerToken:)
252252
public static func register(token: Data) {
253-
implementation?.register(token: token)
253+
register(token: token, onSuccess: nil, onFailure: nil)
254254
}
255255

256256
/// Register this device's token with Iterable
@@ -268,7 +268,8 @@ import UIKit
268268
/// - SeeAlso: IterableConfig, OnSuccessHandler, OnFailureHandler
269269
@objc(registerToken:onSuccess:OnFailure:)
270270
public static func register(token: Data, onSuccess: OnSuccessHandler? = nil, onFailure: OnFailureHandler? = nil) {
271-
implementation?.register(token: token, onSuccess: onSuccess, onFailure: onFailure)
271+
guard let implementation, implementation.isSDKInitialized() else { return }
272+
implementation.register(token: token, onSuccess: onSuccess, onFailure: onFailure)
272273
}
273274

274275
@objc(pauseAuthRetries:)
@@ -331,10 +332,12 @@ import UIKit
331332
mergeNestedObjects: Bool,
332333
onSuccess: OnSuccessHandler? = nil,
333334
onFailure: OnFailureHandler? = nil) {
334-
implementation?.updateUser(dataFields,
335-
mergeNestedObjects: mergeNestedObjects,
336-
onSuccess: onSuccess,
337-
onFailure: onFailure)
335+
guard let implementation, implementation.isSDKInitialized() else { return }
336+
337+
implementation.updateUser(dataFields,
338+
mergeNestedObjects: mergeNestedObjects,
339+
onSuccess: onSuccess,
340+
onFailure: onFailure)
338341
}
339342

340343
/// Updates the current user's email
@@ -348,8 +351,17 @@ import UIKit
348351
///
349352
/// - SeeAlso: OnSuccessHandler, OnFailureHandler
350353
@objc(updateEmail:onSuccess:onFailure:)
351-
public static func updateEmail(_ newEmail: String, onSuccess: OnSuccessHandler?, onFailure: OnFailureHandler?) {
352-
implementation?.updateEmail(newEmail, onSuccess: onSuccess, onFailure: onFailure)
354+
public static func updateEmail(
355+
_ newEmail: String,
356+
onSuccess: OnSuccessHandler?,
357+
onFailure: OnFailureHandler?
358+
) {
359+
updateEmail(
360+
newEmail,
361+
withToken: nil,
362+
onSuccess: onSuccess,
363+
onFailure: onFailure
364+
)
353365
}
354366

355367
/// Updates the current user's email, and set the new authentication token
@@ -365,10 +377,17 @@ import UIKit
365377
/// - SeeAlso: OnSuccessHandler, OnFailureHandler
366378
@objc(updateEmail:withToken:onSuccess:onFailure:)
367379
public static func updateEmail(_ newEmail: String,
368-
withToken token: String,
380+
withToken token: String? = nil,
369381
onSuccess: OnSuccessHandler?,
370382
onFailure: OnFailureHandler?) {
371-
implementation?.updateEmail(newEmail, withToken: token, onSuccess: onSuccess, onFailure: onFailure)
383+
guard let implementation, implementation.isSDKInitialized() else { return }
384+
385+
implementation.updateEmail(
386+
newEmail,
387+
withToken: token,
388+
onSuccess: onSuccess,
389+
onFailure: onFailure
390+
)
372391
}
373392

374393
/// Tracks what's in the shopping cart (or equivalent) at this point in time
@@ -379,7 +398,7 @@ import UIKit
379398
/// - SeeAlso: CommerceItem
380399
@objc(updateCart:)
381400
public static func updateCart(items: [CommerceItem]) {
382-
implementation?.updateCart(items: items)
401+
updateCart(items: items, onSuccess: nil, onFailure: nil)
383402
}
384403

385404
/// Tracks what's in the shopping cart (or equivalent) at this point in time
@@ -394,7 +413,9 @@ import UIKit
394413
public static func updateCart(items: [CommerceItem],
395414
onSuccess: OnSuccessHandler?,
396415
onFailure: OnFailureHandler?) {
397-
implementation?.updateCart(items: items, onSuccess: onSuccess, onFailure: onFailure)
416+
guard let implementation, implementation.isSDKInitialized() else { return }
417+
418+
implementation.updateCart(items: items, onSuccess: onSuccess, onFailure: onFailure)
398419
}
399420

400421
/// Tracks a purchase
@@ -406,7 +427,15 @@ import UIKit
406427
/// - SeeAlso: CommerceItem
407428
@objc(trackPurchase:items:)
408429
public static func track(purchase withTotal: NSNumber, items: [CommerceItem]) {
409-
implementation?.trackPurchase(withTotal, items: items)
430+
track(
431+
purchase: withTotal,
432+
items: items,
433+
dataFields: nil,
434+
campaignId: nil,
435+
templateId: nil,
436+
onSuccess: nil,
437+
onFailure: nil
438+
)
410439
}
411440

412441
/// Tracks a purchase with additional data
@@ -419,7 +448,15 @@ import UIKit
419448
/// - SeeAlso: CommerceItem
420449
@objc(trackPurchase:items:dataFields:)
421450
public static func track(purchase withTotal: NSNumber, items: [CommerceItem], dataFields: [AnyHashable: Any]?) {
422-
implementation?.trackPurchase(withTotal, items: items, dataFields: dataFields)
451+
track(
452+
purchase: withTotal,
453+
items: items,
454+
dataFields: dataFields,
455+
campaignId: nil,
456+
templateId: nil,
457+
onSuccess: nil,
458+
onFailure: nil
459+
)
423460
}
424461

425462
/// Tracks a purchase with additional data and custom completion blocks.
@@ -438,11 +475,15 @@ import UIKit
438475
dataFields: [AnyHashable: Any]?,
439476
onSuccess: OnSuccessHandler?,
440477
onFailure: OnFailureHandler?) {
441-
implementation?.trackPurchase(withTotal,
442-
items: items,
443-
dataFields: dataFields,
444-
onSuccess: onSuccess,
445-
onFailure: onFailure)
478+
track(
479+
purchase: withTotal,
480+
items: items,
481+
dataFields: dataFields,
482+
campaignId: nil,
483+
templateId: nil,
484+
onSuccess: onSuccess,
485+
onFailure: onFailure
486+
)
446487
}
447488

448489
/// Tracks a purchase with additional data and custom completion blocks.
@@ -465,13 +506,16 @@ import UIKit
465506
templateId: NSNumber?,
466507
onSuccess: OnSuccessHandler?,
467508
onFailure: OnFailureHandler?) {
468-
implementation?.trackPurchase(withTotal,
469-
items: items,
470-
dataFields: dataFields,
471-
campaignId: campaignId,
472-
templateId: templateId,
473-
onSuccess: onSuccess,
474-
onFailure: onFailure)
509+
510+
guard let implementation, implementation.isSDKInitialized() else { return }
511+
512+
implementation.trackPurchase(withTotal,
513+
items: items,
514+
dataFields: dataFields,
515+
campaignId: campaignId,
516+
templateId: templateId,
517+
onSuccess: onSuccess,
518+
onFailure: onFailure)
475519
}
476520

477521

@@ -628,7 +672,9 @@ import UIKit
628672
subscribedMessageTypeIds: [NSNumber]?,
629673
campaignId: NSNumber?,
630674
templateId: NSNumber?) {
631-
implementation?.updateSubscriptions(emailListIds,
675+
guard let implementation, implementation.isSDKInitialized() else { return }
676+
677+
implementation.updateSubscriptions(emailListIds,
632678
unsubscribedChannelIds: unsubscribedChannelIds,
633679
unsubscribedMessageTypeIds: unsubscribedMessageTypeIds,
634680
subscribedMessageTypeIds: subscribedMessageTypeIds,
@@ -650,12 +696,16 @@ import UIKit
650696

651697
@objc(embeddedMessageClick:buttonIdentifier:clickedUrl:)
652698
public static func track(embeddedMessageClick: IterableEmbeddedMessage, buttonIdentifier: String?, clickedUrl: String) {
653-
implementation?.track(embeddedMessageClick: embeddedMessageClick, buttonIdentifier: buttonIdentifier, clickedUrl: clickedUrl)
699+
guard let implementation, implementation.isSDKInitialized() else { return }
700+
701+
implementation.track(embeddedMessageClick: embeddedMessageClick, buttonIdentifier: buttonIdentifier, clickedUrl: clickedUrl)
654702
}
655703

656704
@objc(embeddedMessageReceived:)
657705
public static func track(embeddedMessageReceived: IterableEmbeddedMessage) {
658-
implementation?.track(embeddedMessageReceived: embeddedMessageReceived)
706+
guard let implementation, implementation.isSDKInitialized() else { return }
707+
708+
implementation.track(embeddedMessageReceived: embeddedMessageReceived)
659709
}
660710

661711
// MARK: In-App Notifications
@@ -672,7 +722,9 @@ import UIKit
672722
/// - SeeAlso: IterableInAppDelegate
673723
@objc(trackInAppOpen:location:)
674724
public static func track(inAppOpen message: IterableInAppMessage, location: InAppLocation = .inApp) {
675-
implementation?.trackInAppOpen(message, location: location)
725+
guard let implementation, implementation.isSDKInitialized() else { return }
726+
727+
implementation.trackInAppOpen(message, location: location)
676728
}
677729

678730
/// Tracks an `InAppClick` event
@@ -686,7 +738,9 @@ import UIKit
686738
/// - clickedUrl: The URL of the button or link that was clicked
687739
@objc(trackInAppClick:location:clickedUrl:)
688740
public static func track(inAppClick message: IterableInAppMessage, location: InAppLocation = .inApp, clickedUrl: String) {
689-
implementation?.trackInAppClick(message, location: location, clickedUrl: clickedUrl)
741+
guard let implementation, implementation.isSDKInitialized() else { return }
742+
743+
implementation.trackInAppClick(message, location: location, clickedUrl: clickedUrl)
690744
}
691745

692746
/// Tracks an `InAppClose` event
@@ -696,7 +750,9 @@ import UIKit
696750
/// - clickedUrl: The url that was clicked to close the in-app. It will be `nil` when the message is closed by clicking `back`.
697751
@objc(trackInAppClose:clickedUrl:)
698752
public static func track(inAppClose message: IterableInAppMessage, clickedUrl: String?) {
699-
implementation?.trackInAppClose(message, clickedUrl: clickedUrl)
753+
guard let implementation, implementation.isSDKInitialized() else { return }
754+
755+
implementation.trackInAppClose(message, clickedUrl: clickedUrl)
700756
}
701757

702758
/// Tracks an `InAppClose` event
@@ -707,7 +763,9 @@ import UIKit
707763
/// - clickedUrl: The URL that was clicked to close the in-app. It will be `nil` when the message is closed by clicking `back`.
708764
@objc(trackInAppClose:location:clickedUrl:)
709765
public static func track(inAppClose message: IterableInAppMessage, location: InAppLocation, clickedUrl: String?) {
710-
implementation?.trackInAppClose(message, location: location, clickedUrl: clickedUrl)
766+
guard let implementation, implementation.isSDKInitialized() else { return }
767+
768+
implementation.trackInAppClose(message, location: location, clickedUrl: clickedUrl)
711769
}
712770

713771
/// Tracks an `InAppClose` event
@@ -719,7 +777,9 @@ import UIKit
719777
/// - clickedUrl: The url that was clicked to close the in-app. It will be `nil` when the message is closed by clicking `back`.
720778
@objc(trackInAppClose:location:source:clickedUrl:)
721779
public static func track(inAppClose message: IterableInAppMessage, location: InAppLocation, source: InAppCloseSource, clickedUrl: String?) {
722-
implementation?.trackInAppClose(message, location: location, source: source, clickedUrl: clickedUrl)
780+
guard let implementation, implementation.isSDKInitialized() else { return }
781+
782+
implementation.trackInAppClose(message, location: location, source: source, clickedUrl: clickedUrl)
723783
}
724784

725785
/// Consumes the notification and removes it from the list of in-app messages
@@ -729,7 +789,9 @@ import UIKit
729789
/// - location: The location from where this message was shown. `inbox` or `inApp`.
730790
@objc(inAppConsume:location:)
731791
public static func inAppConsume(message: IterableInAppMessage, location: InAppLocation = .inApp) {
732-
implementation?.inAppConsume(message: message, location: location)
792+
guard let implementation, implementation.isSDKInitialized() else { return }
793+
794+
implementation.inAppConsume(message: message, location: location)
733795
}
734796

735797
/// Consumes the notification and removes it from the list of in-app messages
@@ -740,7 +802,9 @@ import UIKit
740802
/// - source: The source of deletion `inboxSwipe` or `deleteButton`.
741803
@objc(inAppConsume:location:source:)
742804
public static func inAppConsume(message: IterableInAppMessage, location: InAppLocation = .inApp, source: InAppDeleteSource) {
743-
implementation?.inAppConsume(message: message, location: location, source: source)
805+
guard let implementation, implementation.isSDKInitialized() else { return }
806+
807+
implementation.inAppConsume(message: message, location: location, source: source)
744808
}
745809

746810
/// Tracks analytics data from a session of using an inbox UI

0 commit comments

Comments
 (0)