Skip to content

Commit ea12952

Browse files
move sdkVersion to static init
1 parent d0ddd8f commit ea12952

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

OptimizelySDK/Implementation/Events/BatchEventBuilder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class BatchEventBuilder {
8383

8484
let batchEvent = BatchEvent(revision: config.project.revision,
8585
accountID: config.project.accountId,
86-
clientVersion: Utils.getSDKVersion(),
86+
clientVersion: Utils.sdkVersion,
8787
visitors: [visitor],
8888
projectID: config.project.projectId,
8989
clientName: swiftSdkClientName,

OptimizelySDK/Utils/Utils.swift

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,17 @@ import Foundation
1010

1111
class Utils {
1212

13-
static var sdkVersion:String?
13+
// - Bundle(for: OptimizelyManager.self) not working properly to get SDK bundle within unit tests
14+
// - Bundle(identifier: bundleIdentifier) works ok consistently
15+
// - CocoaPods uses its own bundle identifier, so let it use Bundle(for:) as a fallback
16+
// CocoaPods copies "s.version" in podspec to "CFBundleShortVersionString" in its own Info.plist file
17+
static var sdkVersion:String = {
18+
let bundle = Bundle(identifier: "com.optimizely.OptimizelySwiftSDK") ?? Bundle(for: OptimizelyManager.self)
19+
guard let version = bundle.infoDictionary!["CFBundleShortVersionString"] as? String else {
20+
return "3.0.0"
21+
}
22+
return version
23+
}()
1424

1525
// @objc NSNumber can be casted either Bool, Int, or Double
1626
// more filtering required to avoid NSNumber(false, true) interpreted as Int(0, 1) instead of Bool
@@ -115,25 +125,4 @@ class Utils {
115125
guard isBoolType(value) else { return nil }
116126
return (value as! Bool)
117127
}
118-
119-
static func getSDKVersion() -> String {
120-
121-
// - Bundle(for: OptimizelyManager.self) not working properly to get SDK bundle within unit tests
122-
// - Bundle(identifier: bundleIdentifier) works ok consistently
123-
// - CocoaPods uses its own bundle identifier, so let it use Bundle(for:) as a fallback
124-
// CocoaPods copies "s.version" in podspec to "CFBundleShortVersionString" in its own Info.plist file
125-
if let sdkVersion = sdkVersion {
126-
return sdkVersion
127-
}
128-
129-
let bundle = Bundle(identifier: "com.optimizely.OptimizelySwiftSDK") ?? Bundle(for: OptimizelyManager.self)
130-
guard let version = bundle.infoDictionary!["CFBundleShortVersionString"] as? String else {
131-
fatalError("Check if SDK framework identifier is correct")
132-
}
133-
134-
sdkVersion = version
135-
136-
return version
137-
}
138-
139128
}

0 commit comments

Comments
 (0)