mirror of
https://github.com/aptabase/aptabase-swift.git
synced 2026-01-18 22:21:17 +01:00
use class on objc
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user