Skip to content

Added convenience methods for ObjC interop #142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
Analytics.main.add(plugin: IDFACollection())
Analytics.main.add(plugin: UIKitScreenTracking())
Analytics.main.add(plugin: NotificationTracking())

#if os(iOS) && !targetEnvironment(macCatalyst)
Analytics.main.add(plugin: CellularCarrier())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

#endif

Analytics.support.add(plugin: ConsoleLogger(name: "support"))
Analytics.support.add(plugin: ConsentTracking())
Expand Down
3 changes: 3 additions & 0 deletions Examples/other_plugins/CellularCarrier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#if os(iOS) && !targetEnvironment(macCatalyst)

import Foundation
import Segment
import CoreTelephony
Expand Down Expand Up @@ -123,3 +125,4 @@ class PrimaryCellularCarrier: Plugin {
}()
}

#endif
8 changes: 8 additions & 0 deletions Sources/Segment/Analytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ extension Analytics {
return nil
}

/// Returns the traits that were specified in the last identify call, as a dictionary.
public func traits() -> [String: Any]? {
if let userInfo: UserInfo = store.currentState() {
return userInfo.traits?.dictionaryValue
}
return nil
}

/// Tells this instance of Analytics to flush any queued events up to Segment.com. This command will also
/// be sent to each plugin present in the system.
public func flush() {
Expand Down
16 changes: 10 additions & 6 deletions Sources/Segment/ObjC/ObjCAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ import Foundation

@objc(SEGAnalytics)
public class ObjCAnalytics: NSObject {
internal let analytics: Analytics
/// The underlying Analytics object we're working with
public let analytics: Analytics
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had to expose this for tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes cross-config way easier if you can init in objc and then access it later in swift if needed and vice versa.


@objc
public init(configuration: ObjCConfiguration) {
self.analytics = Analytics(configuration: configuration.configuration)
}

/// Get a workable ObjC instance by wrapping a Swift instance
/// Useful when you want additional flexibility or to share
/// a single instance between ObjC<>Swift.
public init(wrapping analytics: Analytics) {
self.analytics = analytics
}
}

// MARK: - ObjC Events
Expand Down Expand Up @@ -127,11 +135,7 @@ extension ObjCAnalytics {

@objc
public func traits() -> [String: Any]? {
var traits: [String: Any]? = nil
if let userInfo: UserInfo = analytics.store.currentState() {
traits = userInfo.traits?.dictionaryValue
}
return traits
return analytics.traits()
}

@objc
Expand Down
6 changes: 6 additions & 0 deletions Tests/Segment-Tests/ObjC_Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ class ObjC_Tests: XCTestCase {

*/

func testWrapping() {
let a = Analytics(configuration: Configuration(writeKey: "WRITE_KEY"))
let objc = ObjCAnalytics(wrapping: a)

XCTAssertTrue(objc.analytics === a)
}

func testNonTrivialConfiguration() {
let config = ObjCConfiguration(writeKey: "WRITE_KEY")
Expand Down