Skip to content

Commit ca8668c

Browse files
bsneedBrandon Sneed
and
Brandon Sneed
authored
Added convenience methods for ObjC interop (#142)
* ObjC convenience additions * Narrow carrier info example down to iOS * Made existing objc traits use new dictionary traits. * Lets try that ci fix again. :S * one more time. Co-authored-by: Brandon Sneed <[email protected]>
1 parent 4be02b6 commit ca8668c

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

Examples/apps/SegmentUIKitExample/SegmentUIKitExample/AppDelegate.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1818
Analytics.main.add(plugin: IDFACollection())
1919
Analytics.main.add(plugin: UIKitScreenTracking())
2020
Analytics.main.add(plugin: NotificationTracking())
21+
22+
#if os(iOS) && !targetEnvironment(macCatalyst)
2123
Analytics.main.add(plugin: CellularCarrier())
24+
#endif
2225

2326
Analytics.support.add(plugin: ConsoleLogger(name: "support"))
2427
Analytics.support.add(plugin: ConsentTracking())

Examples/other_plugins/CellularCarrier.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3333
// SOFTWARE.
3434

35+
#if os(iOS) && !targetEnvironment(macCatalyst)
36+
3537
import Foundation
3638
import Segment
3739
import CoreTelephony
@@ -123,3 +125,4 @@ class PrimaryCellularCarrier: Plugin {
123125
}()
124126
}
125127

128+
#endif

Sources/Segment/Analytics.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ extension Analytics {
9595
return nil
9696
}
9797

98+
/// Returns the traits that were specified in the last identify call, as a dictionary.
99+
public func traits() -> [String: Any]? {
100+
if let userInfo: UserInfo = store.currentState() {
101+
return userInfo.traits?.dictionaryValue
102+
}
103+
return nil
104+
}
105+
98106
/// Tells this instance of Analytics to flush any queued events up to Segment.com. This command will also
99107
/// be sent to each plugin present in the system.
100108
public func flush() {

Sources/Segment/ObjC/ObjCAnalytics.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@ import Foundation
1313

1414
@objc(SEGAnalytics)
1515
public class ObjCAnalytics: NSObject {
16-
internal let analytics: Analytics
16+
/// The underlying Analytics object we're working with
17+
public let analytics: Analytics
1718

1819
@objc
1920
public init(configuration: ObjCConfiguration) {
2021
self.analytics = Analytics(configuration: configuration.configuration)
2122
}
23+
24+
/// Get a workable ObjC instance by wrapping a Swift instance
25+
/// Useful when you want additional flexibility or to share
26+
/// a single instance between ObjC<>Swift.
27+
public init(wrapping analytics: Analytics) {
28+
self.analytics = analytics
29+
}
2230
}
2331

2432
// MARK: - ObjC Events
@@ -127,11 +135,7 @@ extension ObjCAnalytics {
127135

128136
@objc
129137
public func traits() -> [String: Any]? {
130-
var traits: [String: Any]? = nil
131-
if let userInfo: UserInfo = analytics.store.currentState() {
132-
traits = userInfo.traits?.dictionaryValue
133-
}
134-
return traits
138+
return analytics.traits()
135139
}
136140

137141
@objc

Tests/Segment-Tests/ObjC_Tests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ class ObjC_Tests: XCTestCase {
2727

2828
*/
2929

30+
func testWrapping() {
31+
let a = Analytics(configuration: Configuration(writeKey: "WRITE_KEY"))
32+
let objc = ObjCAnalytics(wrapping: a)
33+
34+
XCTAssertTrue(objc.analytics === a)
35+
}
3036

3137
func testNonTrivialConfiguration() {
3238
let config = ObjCConfiguration(writeKey: "WRITE_KEY")

0 commit comments

Comments
 (0)