-
Notifications
You must be signed in to change notification settings - Fork 22
/
UserAttributes.swift
103 lines (95 loc) · 3.96 KB
/
UserAttributes.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
// PublicEvents.swift
// Superwall
//
// Created by Yusuf Tör on 04/03/2022.
//
import Foundation
extension Superwall {
/// Sets user attributes for use in paywalls and the Superwall dashboard.
///
/// If the existing user attributes dictionary already has a value for a given property, the old
/// value is overwritten. Existing properties will not be affected.
/// Useful for analytics and conditional paywall rules you may define in the Superwall Dashboard.
/// They should **not** be used as a source of truth for sensitive information.
///
/// Here's how you might set user attributes after retrieving your user's data:
/// ```swift
/// var attributes: [String: Any] = [
/// "name": user.name,
/// "apnsToken": user.apnsTokenString,
/// "email": user.email,
/// "username": user.username,
/// "profilePic": user.profilePicUrl
/// ]
/// await Superwall.shared.setUserAttributes(attributes)
/// ```
/// See <doc:SettingUserAttributes> for more.
///
/// - Parameter attributes: A `[String: Any?]` dictionary used to describe any custom
/// attributes you'd like to store for the user. Values can be any JSON encodable value, `URL`s or `Date`s.
/// Note: Keys beginning with `$` are reserved for Superwall and will be dropped. Arrays and dictionaries
/// as values are not supported at this time, and will be dropped.
public func setUserAttributes(_ attributes: [String: Any?]) {
mergeAttributes(attributes)
}
/// The Objective-C method for setting user attributes for use in your paywalls and the dashboard.
///
/// If the existing user attributes dictionary already has a value for a given property, the old
/// value is overwritten. Existing properties will not be affected.
/// Useful for analytics and conditional paywall rules you may define in the Superwall Dashboard.
/// They should **not** be used as a source of truth for sensitive information.
///
/// Here's how you might set user attributes after retrieving your user's data:
///
/// ```
/// NSDictionary *userAttributes = @{ key : value, key2 : value2};
/// [[Superwall sharedInstance] setUserAttributes:userAttributes];
/// ```
///
/// - Parameters:
/// - attributes: An `NSDictionary` used to describe any custom
/// attributes you'd like to store for the user. Values can be any JSON encodable value, `URL`s or `Date`s.
/// Note: Keys beginning with `$` are reserved for Superwall and will be dropped. Arrays and dictionaries
/// as values are not supported at this time, and will be dropped.
@available(swift, obsoleted: 1.0)
@objc public func setUserAttributes(_ attributes: NSDictionary) {
var swiftDictionary: [String: Any?] = [:]
let keys = attributes.allKeys.compactMap { $0 as? String }
for key in keys {
let keyValue = attributes.value(forKey: key) as Any?
swiftDictionary[key] = keyValue
}
mergeAttributes(swiftDictionary)
}
/// The Objective-C method for removing user attributes for use in your paywalls and the dashboard.
///
/// Example:
/// ```
/// [[Superwall sharedInstance] removeUserAttributes:@[@"key1", @"key2"]];
/// ```
///
/// - Parameter keys: An array containing the keys you wish to remove from the user attributes dictionary.
@available(swift, obsoleted: 1.0)
@objc public func removeUserAttributes(_ keys: [String]) {
let userAttributes: [String: Any?] = keys.reduce([:]) { dictionary, key in
var dictionary = dictionary
dictionary[key] = Any?.none
return dictionary
}
setUserAttributes(userAttributes)
}
private func mergeAttributes(_ attributes: [String: Any?]) {
var customAttributes: [String: Any?] = [:]
for key in attributes.keys {
if let value = attributes[key] {
if key.starts(with: "$") {
// preserve $ for Superwall-only values
continue
}
customAttributes[key] = value
}
}
dependencyContainer.identityManager.mergeUserAttributes(customAttributes)
}
}