Skip to main content

SDK Methods Reference

Complete reference for all public methods and types in the Positive User iOS SDK.


UserSDK​

Initialization​

UserSDK(
application: UIApplication,
apiKey: String,
baseURL: String,
shouldTrackActivities: Bool
)

Creates the SDK instance and stores it as UserSDK.default. Call once in application(_:didFinishLaunchingWithOptions:).

Static properties​

PropertyTypeDescription
UserSDK.defaultUserSDK?Shared SDK instance. Set automatically on init.
UserSDK.inAppAlertCloseButtonTextStringLabel for the close button on in-app messages. Default: "Close". Set before initializing the SDK.

Instance properties​

PropertyTypeDescription
apiKeyStringRead-only. The API key provided at initialization.
baseURLStringRead-only. The base URL provided at initialization.
userIdString?Read-only. The Positive User-generated contact identifier. Available after the first successful ping().
notificationDelegateRemoteNotificationDelegate?Receives push notification tap events.
fontResolverFontResolving?Provides custom fonts for in-app messages.
inAppNotificationClickDelegateInAppNotificationClickDelegate?Intercepts button taps in in-app messages.

Contact methods​

ping()​

func ping(_ completion: ((Bool, Error?) -> Void)? = nil)

Creates or updates the contact profile with current device data. Call this to start an anonymous session or after updating contact attributes.


setUserData(_:_:)​

func setUserData(_ userData: [UserDataKey: String?], _ completion: ((Bool, Error?) -> Void)? = nil)

Sets standard contact attributes and calls ping(). Pass nil for a key to remove a previously set value.

UserDataKey values:

CaseField sent
.firstNamefirst_name
.lastNamelast_name
.emailemail
.phonephone
.userIduser_id

setCustomUserData(_:_:)​

func setCustomUserData(_ userData: [String: Any?], _ completion: ((Bool, Error?) -> Void)? = nil)

Merges custom attributes into the contact profile and calls ping(). Pass nil for a key to remove a previously set value.


logout(_:)​

func logout(_ completion: ((Bool, Error?) -> Void)? = nil)

Clears all stored contact data and creates a new anonymous session. Local data is wiped immediately regardless of network state.


Event methods​

sendEvent(with:params:_:)​

func sendEvent(with name: String, params: [String: Any], _ completion: ((Bool, Error?) -> Void)? = nil)

Sends a custom event. Requires an identified contact - call ping() first.


sendProductEvent(_:eventType:name:productURL:params:_:)​

func sendProductEvent(
_ productId: String,
eventType: EventType,
name: String? = nil,
productURL: String? = nil,
params: [String: Any]? = nil,
_ completion: ((Bool, Error?) -> Void)? = nil
)

Sends a product event. Requires an identified contact.

EventType cases: .addToCart, .purchase, .liking, .addToObservation, .order, .reservation, .return, .view, .click, .detail, .add, .remove, .checkout, .checkoutOption, .refund, .promoClick

See Product Events for the full reference.


trackScreen(with:_:)​

func trackScreen(with name: String, _ completion: ((Bool, Error?) -> Void)? = nil)

Records a screen visit. Requires an identified contact.


Notification methods​

registerForRemoteNotifications(options:notificationDelegate:)​

func registerForRemoteNotifications(
options: UNAuthorizationOptions,
notificationDelegate: RemoteNotificationDelegate? = nil
)

Requests push notification permission and registers the device with FCM.


handleNotification(userInfo:)​

func handleNotification(userInfo: [AnyHashable: Any])

Forwards a notification payload to the SDK. Call from application(_:didReceiveRemoteNotification:fetchCompletionHandler:) in your AppDelegate to handle background notification taps.


Protocols​

RemoteNotificationDelegate​

protocol RemoteNotificationDelegate {
func didReceiveRemoteNotification(with id: String, data: [AnyHashable: Any])
}

Called when a contact taps a push notification. Fires for both Positive User notifications and all other FCM notifications.


InAppNotificationClickDelegate​

protocol InAppNotificationClickDelegate: AnyObject {
func inAppNotificationDidClick(url: URL) -> Bool
}

Called when a contact taps a button in an in-app message. Return true to handle the URL yourself, false to let the SDK open it in the browser.


FontResolving​

@objc protocol FontResolving {
func resolveFontFor(name: String, size: CGFloat) -> UIFont?
}

Provides custom font instances for in-app message rendering. Return nil to fall back to the system font.


TrackableView​

protocol TrackableView {
var screenName: String { get }
}

Implemented by UIViewController by default (using the class name). Override screenName to provide a custom value.


Trackable view controllers​

Ready-made base classes that call trackScreen automatically in viewDidLoad:

ClassExtends
TrackableViewControllerUIViewController
TrackableTableViewControllerUITableViewController
TrackableCollectionViewControllerUICollectionViewController
TrackablePageViewControllerUIPageViewController
TrackableSplitViewControllerUISplitViewController

NotificationExtensionHelper​

Used in a Notification Service Extension to attach images to push notifications.

NotificationExtensionHelper.shared.fillNotificationContent(
_ content: UNMutableNotificationContent?,
contentHandler: @escaping (UNNotificationContent) -> Void
)

See Push Notifications for the full setup.