use class on objc

This commit is contained in:
goenning
2023-06-20 21:42:33 +01:00
parent 7bba2314a7
commit 357e11d458
2 changed files with 30 additions and 27 deletions

View File

@@ -25,7 +25,8 @@
[[Aptabase shared] initializeWithAppKey:@"A-DEV-0000000000"];
// Using self hosted APP key
// [[Aptabase shared] initializeWithAppKey:@"A-SH-0000000000" parameters:@{@"host":@"https://yourdomain.com"}];
// InitOptions *opts = [[InitOptions alloc] initWithHost: @"https://aptabase.yourserver.com"];
// [[Aptabase shared] initializeWithAppKey:@"A-SH-0000000000" options: opts];
return YES;
}

View File

@@ -1,15 +1,14 @@
import Foundation
public struct InitOptions {
public final class InitOptions: NSObject {
let host: String?
public init(host: String? = nil) {
@objc public init(host: String? = nil) {
self.host = host
}
}
// The Aptabase client used to track events
@objcMembers
public class Aptabase: NSObject {
private static var sdkVersion = "aptabase-swift@0.1.0";
@@ -21,7 +20,7 @@ public class Aptabase: NSObject {
private var lastTouched = Date()
private var apiURL: URL?
public static let shared = Aptabase()
@objc public static let shared = Aptabase()
private var hosts = [
"US": "https://us.aptabase.com",
@@ -51,34 +50,24 @@ public class Aptabase: NSObject {
env = EnvironmentInfo.get()
}
public func initialize(appKey: String, parameters: [String:String]?) {
if let params = parameters, let host = params["host"] {
initialize(appKey: appKey, with: InitOptions(host: host))
}else{
initialize(appKey: appKey, with: nil)
}
// Track an event using given properties
public func trackEvent(_ eventName: String, with props: [String: Value] = [:]) {
sendEvent(eventName, with: props)
}
public func initialize(appKey: String) {
@objc public func initialize(appKey: String) {
initialize(appKey: appKey, with: nil)
}
private func getApiUrl(_ region: String, _ host: String?) -> URL? {
guard var baseURL = hosts[region] else { return nil }
if region == "SH" {
guard let host else {
debugPrint("Host parameter must be defined when using Self-Hosted App Key. Tracking will be disabled.")
return nil
}
baseURL = host
}
return URL(string: "\(baseURL)/api/v0/event")
@objc public func initialize(appKey: String, options: InitOptions?) {
initialize(appKey: appKey, with: options)
}
// Track an event and its properties
public func trackEvent(_ eventName: String, with props: [String: Any] = [:]) {
@objc public func trackEvent(_ eventName: String, with props: [String: Any] = [:]) {
sendEvent(eventName, with: props)
}
private func sendEvent(_ eventName: String, with props: [String: Any] = [:]) {
DispatchQueue(label: "com.aptabase.aptabase").async { [self] in
guard let appKey, let env, let apiURL else {
return
@@ -136,4 +125,17 @@ public class Aptabase: NSObject {
task.resume()
}
}
private func getApiUrl(_ region: String, _ host: String?) -> URL? {
guard var baseURL = hosts[region] else { return nil }
if region == "SH" {
guard let host else {
debugPrint("Host parameter must be defined when using Self-Hosted App Key. Tracking will be disabled.")
return nil
}
baseURL = host
}
return URL(string: "\(baseURL)/api/v0/event")
}
}