Skip to content
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

Support Device Model #21

Merged
merged 2 commits into from Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion Sources/Aptabase/AptabaseClient.swift
Expand Up @@ -36,7 +36,8 @@ class AptabaseClient {
osVersion: env.osVersion,
appVersion: env.appVersion,
appBuildNumber: env.appBuildNumber,
sdkVersion: AptabaseClient.sdkVersion
sdkVersion: AptabaseClient.sdkVersion,
deviceModel: env.deviceModel
),
props: props)
dispatcher.enqueue(evt)
Expand Down
20 changes: 19 additions & 1 deletion Sources/Aptabase/EnvironmentInfo.swift
Expand Up @@ -17,6 +17,7 @@ struct EnvironmentInfo {
var locale = ""
var appVersion = ""
var appBuildNumber = ""
var deviceModel = ""

static func current() -> EnvironmentInfo {
let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
Expand All @@ -28,7 +29,8 @@ struct EnvironmentInfo {
osVersion: osVersion,
locale: Locale.current.languageCode ?? "",
appVersion: appVersion ?? "",
appBuildNumber: appBuildNumber ?? ""
appBuildNumber: appBuildNumber ?? "",
deviceModel: deviceModel
)
}

Expand Down Expand Up @@ -71,4 +73,20 @@ struct EnvironmentInfo {
""
#endif
}

private static var deviceModel: String {
if let simulatorModelIdentifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] {
return simulatorModelIdentifier
} else {
var systemInfo = utsname()
uname(&systemInfo)

let identifier = withUnsafePointer(to: &systemInfo.machine) { ptr in
Copy link
Contributor Author

@manucheri manucheri Mar 22, 2024

Choose a reason for hiding this comment

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

We are using pointers here since the API to get the device model is a Unix (C based) API. Would be nice to have a native Swift/Obj-C API for this, but as far as I could find that is not available.

Copy link
Member

Choose a reason for hiding this comment

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

Im ok with that, but is there anyway to safeguard this from an error/exception? I don’t know much about Swift :)

I’d rather the have an empty model than seeing apps crashing because of errors around this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can't say I am an expert on the Swift <-> C interop myself.

But basically since we are dealing with C pointers (which are inherently "unsafe") we need to make sure that the memory we are accessing is correct (which it should be due to us simply using the Unix APIs here to read the data), and not messed with, to avoid undefined behavior.

The usage of withUnsafePointer ensures we only read, and will not manipulate, the pointer. What we might get - I guess - if something changes the structure or behavior of utsname (system info) is "unexpected behavior". But since we are not doing that we should be safe.

I'll add a check that uname returned successfully (which if I remember correctly from C is a return value of 0). I also think due to this being a static function called only once when we initialise the library we won't have the issue with something accessing the pointer at the same time.
So I don't really see the need for additional error checking here (famous last words maybe 😅).

ptr.withMemoryRebound(to: CChar.self, capacity: 1) { machinePtr in
String(cString: machinePtr)
}
}
return identifier
}
}
}
1 change: 1 addition & 0 deletions Sources/Aptabase/EventDispatcher.swift
Expand Up @@ -15,6 +15,7 @@ struct Event: Encodable {
var appVersion: String
var appBuildNumber: String
var sdkVersion: String
var deviceModel: String
}
}

Expand Down
6 changes: 4 additions & 2 deletions Tests/AptabaseTests/EventDispatcherTests.swift
Expand Up @@ -21,7 +21,8 @@ final class EventDispatcherTests: XCTestCase {
isDebug: true,
osName: "iOS",
osVersion: "17.0",
appVersion: "1.0.0"
appVersion: "1.0.0",
deviceModel: "iPhone16,2"
)

override func setUp() {
Expand Down Expand Up @@ -90,7 +91,8 @@ final class EventDispatcherTests: XCTestCase {
osVersion: env.osVersion,
appVersion: env.appVersion,
appBuildNumber: env.appBuildNumber,
sdkVersion: "aptabase-swift@0.0.0")
sdkVersion: "aptabase-swift@0.0.0",
deviceModel: env.deviceModel)
)
}
}