Skip to content

Commit

Permalink
[MOB-10951] Add mobile framework info
Browse files Browse the repository at this point in the history
  • Loading branch information
sumeruchat committed Jan 13, 2025
1 parent cfc4bf8 commit 742b950
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 74 deletions.
71 changes: 5 additions & 66 deletions swift-sdk/Internal/InternalIterableAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,18 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {

hexToken = token.hexString()

let mobileFrameworkType = IterableAPIMobileFrameworkType.detect()
let mobileFrameworkInfo = config.mobileFrameworkInfo ?? IterableAPIMobileFrameworkInfo(
frameworkType: IterableAPIMobileFrameworkDetector.frameworkType(),
iterableSdkVersion: nil
)
let registerTokenInfo = RegisterTokenInfo(hexToken: token.hexString(),
appName: appName,
pushServicePlatform: config.pushPlatform,
apnsType: dependencyContainer.apnsTypeChecker.apnsType,
deviceId: deviceId,
deviceAttributes: deviceAttributes,
sdkVersion: localStorage.sdkVersion,
mobileFrameworkInfo: IterableAPIMobileFrameworkInfo(frameworkType: mobileFrameworkType, iterableSdkVersion: nil))
mobileFrameworkInfo: mobileFrameworkInfo)
requestHandler.register(registerTokenInfo: registerTokenInfo,
notificationStateProvider: notificationStateProvider,
onSuccess: { (_ data: [AnyHashable: Any]?) in
Expand Down Expand Up @@ -779,67 +782,3 @@ final class InternalIterableAPI: NSObject, PushTrackerProtocol, AuthProvider {
}
}

extension IterableAPIMobileFrameworkType {
private struct FrameworkClasses {
static let flutter = [
"FlutterViewController",
"GeneratedPluginRegistrant",
"FlutterEngine",
"FlutterPluginRegistry"
]

static let reactNative = [
"RCTBridge",
"RCTRootView",
"RCTBundleURLProvider",
"RCTEventEmitter"
]
}

static func detect() -> IterableAPIMobileFrameworkType {
let bundle = Bundle.main

// Helper function to check for framework classes
func hasFrameworkClasses(_ classNames: [String]) -> Bool {
// Consider framework present if ANY of its core classes are found
return classNames.contains { className in
bundle.classNamed(className) != nil
}
}

// Check for multiple framework classes to increase confidence
let hasFlutter = hasFrameworkClasses(FrameworkClasses.flutter)
let hasReactNative = hasFrameworkClasses(FrameworkClasses.reactNative)

switch (hasFlutter, hasReactNative) {
case (true, true):
ITBError("Both Flutter and React Native frameworks detected. This is unexpected.")
// In case of ambiguity, we could try to determine the primary framework
// by checking for more framework-specific indicators
if let mainBundle = Bundle.main.infoDictionary,
mainBundle["CFBundleExecutable"] as? String == "Runner" {
return .flutter // Flutter apps typically use "Runner" as executable name
}
return .reactNative

case (true, false):
return .flutter

case (false, true):
return .reactNative

case (false, false):
// Additional check for framework-specific build settings or Info.plist entries
if let mainBundle = Bundle.main.infoDictionary {
if mainBundle["FlutterDeploymentTarget"] != nil {
return .flutter
}
if mainBundle["RNBundleURLProvider"] != nil {
return .reactNative
}
}

return .native
}
}
}
69 changes: 69 additions & 0 deletions swift-sdk/Internal/IterableAPIMobileFrameworkDetector.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Foundation

public final class IterableAPIMobileFrameworkDetector {
private struct FrameworkClasses {
static let flutter = [
"FlutterViewController",
"GeneratedPluginRegistrant",
"FlutterEngine",
"FlutterPluginRegistry"
]

static let reactNative = [
"RCTBridge",
"RCTRootView",
"RCTBundleURLProvider",
"RCTEventEmitter"
]
}

private static var cachedFrameworkType: IterableAPIMobileFrameworkType = {
detectFramework()
}()

static func detectFramework() -> IterableAPIMobileFrameworkType {
let bundle = Bundle.main

// Helper function to check for framework classes
func hasFrameworkClasses(_ classNames: [String]) -> Bool {
classNames.contains { className in
bundle.classNamed(className) != nil
}
}

let hasFlutter = hasFrameworkClasses(FrameworkClasses.flutter)
let hasReactNative = hasFrameworkClasses(FrameworkClasses.reactNative)

switch (hasFlutter, hasReactNative) {
case (true, true):
ITBError("Both Flutter and React Native frameworks detected. This is unexpected.")
if let mainBundle = Bundle.main.infoDictionary,
mainBundle["CFBundleExecutable"] as? String == "Runner" {
return .flutter
}
return .reactNative

case (true, false):
return .flutter

case (false, true):
return .reactNative

case (false, false):
if let mainBundle = Bundle.main.infoDictionary {
if mainBundle["FlutterDeploymentTarget"] != nil {
return .flutter
}
if mainBundle["RNBundleURLProvider"] != nil {
return .reactNative
}
}

return .native
}
}

public static func frameworkType() -> IterableAPIMobileFrameworkType {
return cachedFrameworkType
}
}
11 changes: 4 additions & 7 deletions swift-sdk/Internal/api-client/Request/RequestCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,12 @@ struct RequestCreator {
apnsType: registerTokenInfo.apnsType),
JsonKey.applicationName: registerTokenInfo.appName,
JsonKey.dataFields: dataFields,
JsonKey.mobileFramework: [
JsonKey.frameworkType: registerTokenInfo.mobileFrameworkInfo.frameworkType,
JsonKey.iterableSdkVersion: registerTokenInfo.mobileFrameworkInfo]

]

// Add mobile framework info if available
if let frameworkInfo = registerTokenInfo.mobileFrameworkInfo {
deviceDictionary[JsonKey.mobileFramework] = [
JsonKey.frameworkType: frameworkInfo.frameworkType,
JsonKey.iterableSdkVersion: frameworkInfo.iterableSdkVersion
]
}

var body = [AnyHashable: Any]()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct RegisterTokenInfo {
let deviceId: String
let deviceAttributes: [String: String]
let sdkVersion: String?
let mobileFrameworkInfo: IterableAPIMobileFrameworkInfo?
let mobileFrameworkInfo: IterableAPIMobileFrameworkInfo
}

struct UpdateSubscriptionsInfo {
Expand Down

0 comments on commit 742b950

Please sign in to comment.