Skip to content

Commit

Permalink
Remove UIKit dependency (#90)
Browse files Browse the repository at this point in the history
Replace use of UIDevice and use a different non-UIKit source
  • Loading branch information
andrewjl-mux committed Aug 23, 2023
1 parent 487f616 commit 1d525fe
Showing 1 changed file with 51 additions and 16 deletions.
67 changes: 51 additions & 16 deletions Sources/MuxUploadSDK/InternalUtilities/Reporting/Reporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,36 @@
//

import Foundation
import UIKit

fileprivate func processInfoOperationSystemVersion() -> String {
let version = ProcessInfo().operatingSystemVersion
return "\(version.majorVersion).\(version.minorVersion).\(version.patchVersion)"
}

fileprivate func posixModelName() -> String {
var systemName = utsname()
uname(&systemName)
return withUnsafePointer(to: &systemName.machine) {
$0.withMemoryRebound(to: CChar.self, capacity: 1) {
ptr in String.init(validatingUTF8: ptr)
}
} ?? "Unknown"
}

fileprivate func inferredPlatformName() -> String {
let modelName = posixModelName()
if modelName.contains("ipad") {
return "iPadOS"
} else if modelName.contains("iphone") {
return "iOS"
} else {
#if targetEnvironment(simulator)
return "Simulator"
#else
return "Unknown"
#endif
}
}

class Reporter: NSObject {

Expand All @@ -28,9 +57,11 @@ class Reporter: NSObject {
var locale: Locale {
Locale.current
}
var device: UIDevice {
UIDevice.current
}

let model: String
let platformName: String
let platformVersion: String

var regionCode: String? {
if #available(iOS 16, *) {
return locale.language.region?.identifier
Expand All @@ -52,6 +83,10 @@ class Reporter: NSObject {
string: "https://mobile.muxanalytics.com"
)!

self.model = posixModelName()
self.platformName = inferredPlatformName()
self.platformVersion = processInfoOperationSystemVersion()

super.init()

let sessionConfig: URLSessionConfiguration = URLSessionConfiguration.default
Expand Down Expand Up @@ -105,12 +140,12 @@ extension Reporter {
let data = UploadSucceededEvent.Data(
appName: Bundle.main.appName,
appVersion: Bundle.main.appVersion,
deviceModel: device.model,
deviceModel: model,
inputDuration: inputDuration,
inputSize: inputSize,
inputStandardizationRequested: options.inputStandardization.isRequested,
platformName: device.systemName,
platformVersion: device.systemVersion,
platformName: platformName,
platformVersion: platformVersion,
regionCode: regionCode,
sdkVersion: Version.versionString,
uploadStartTime: uploadStartTime,
Expand Down Expand Up @@ -145,13 +180,13 @@ extension Reporter {
let data = UploadFailedEvent.Data(
appName: Bundle.main.appName,
appVersion: Bundle.main.appVersion,
deviceModel: device.model,
deviceModel: model,
errorDescription: errorDescription,
inputDuration: inputDuration,
inputSize: inputSize,
inputStandardizationRequested: options.inputStandardization.isRequested,
platformName: device.systemName,
platformVersion: device.systemVersion,
platformName: platformName,
platformVersion: platformVersion,
regionCode: regionCode,
sdkVersion: Version.versionString,
uploadStartTime: uploadStartTime,
Expand Down Expand Up @@ -186,13 +221,13 @@ extension Reporter {
let data = InputStandardizationSucceededEvent.Data(
appName: Bundle.main.appName,
appVersion: Bundle.main.appVersion,
deviceModel: device.model,
deviceModel: model,
inputDuration: inputDuration,
inputSize: inputSize,
maximumResolution: options.inputStandardization.maximumResolution.description,
nonStandardInputReasons: nonStandardInputReasons.map(\.description),
platformName: device.systemName,
platformVersion: device.systemVersion,
platformName: platformName,
platformVersion: platformVersion,
regionCode: regionCode,
sdkVersion: Version.versionString,
standardizationStartTime: standardizationStartTime,
Expand Down Expand Up @@ -229,14 +264,14 @@ extension Reporter {
let data = InputStandardizationFailedEvent.Data(
appName: Bundle.main.appName,
appVersion: Bundle.main.appVersion,
deviceModel: device.model,
deviceModel: model,
errorDescription: errorDescription,
inputDuration: inputDuration,
inputSize: inputSize,
maximumResolution: options.inputStandardization.maximumResolution.description,
nonStandardInputReasons: nonStandardInputReasons.map(\.description),
platformName: device.systemName,
platformVersion: device.systemVersion,
platformName: platformName,
platformVersion: platformVersion,
regionCode: regionCode,
sdkVersion: Version.versionString,
standardizationStartTime: standardizationStartTime,
Expand Down

0 comments on commit 1d525fe

Please sign in to comment.