From 53d375b9ea09b5e8dab123c128a13e219b9df0e5 Mon Sep 17 00:00:00 2001 From: Desu Sai Venkat Date: Fri, 2 Jun 2023 11:26:30 +0530 Subject: [PATCH 1/4] feat: added support for sending carrier names on iOS versions < 16 instead of sending un-available --- .../SampleObjC-iOS.xcodeproj/project.pbxproj | 4 +- .../Platforms/Vendors/AppleUtils.swift | 86 +++++++++++++------ 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/Examples/SampleObjC-iOS/SampleObjC-iOS.xcodeproj/project.pbxproj b/Examples/SampleObjC-iOS/SampleObjC-iOS.xcodeproj/project.pbxproj index 3b86bba7..06b9feec 100644 --- a/Examples/SampleObjC-iOS/SampleObjC-iOS.xcodeproj/project.pbxproj +++ b/Examples/SampleObjC-iOS/SampleObjC-iOS.xcodeproj/project.pbxproj @@ -370,7 +370,7 @@ CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 1; - DEVELOPMENT_TEAM = Z6DGB573C6; + DEVELOPMENT_TEAM = WPX9KRKA8B; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = "SampleObjC-iOS/Info.plist"; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; @@ -403,7 +403,7 @@ CLANG_ENABLE_MODULES = YES; CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = 1; - DEVELOPMENT_TEAM = Z6DGB573C6; + DEVELOPMENT_TEAM = WPX9KRKA8B; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_FILE = "SampleObjC-iOS/Info.plist"; INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; diff --git a/Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift b/Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift index b1750149..03a0b258 100644 --- a/Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift +++ b/Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift @@ -12,6 +12,7 @@ import Foundation #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) import SystemConfiguration +import CoreTelephony import UIKit #if !os(tvOS) import WebKit @@ -25,15 +26,15 @@ internal class PhoneVendor: Vendor { } override var type: String { - #if os(iOS) +#if os(iOS) return "ios" - #elseif os(tvOS) +#elseif os(tvOS) return "tvos" - #elseif targetEnvironment(macCatalyst) +#elseif targetEnvironment(macCatalyst) return "macos" - #else +#else return "unknown" - #endif +#endif } override var model: String { @@ -71,6 +72,41 @@ internal class PhoneVendor: Vendor { return [RSiOSLifecycleMonitor(), RSDeviceTokenPlugin()] } + override var carrier: String { +#if !os(iOS) + return "unavailable" +#endif + return retrieveCarrierNames() ?? "unavailable"; + } + + func retrieveCarrierNames() -> String? { + if #available(iOS 16, *) { + RSClient.rsLog(message: "Unable to retrieve carrier name", logLevel: .warning) + return nil + } else if #available(iOS 12, *) { + let networkInfo = CTTelephonyNetworkInfo() + var carrierNames: [String] = [] + + if let carriers = networkInfo.serviceSubscriberCellularProviders?.values { + for carrier in carriers { + if let carrierName = carrier.carrierName, carrierName != "--" { + carrierNames.append(carrierName) + } + } + } + if(!carrierNames.isEmpty) { + let formattedCarrierNames = carrierNames.joined(separator: ", ") + return formattedCarrierNames + } + } else { + let networkInfo = CTTelephonyNetworkInfo() + if let carrier = networkInfo.subscriberCellularProvider?.carrierName, carrier != "--" { + return carrier + } + } + return nil + } + private func deviceModel() -> String { var name: [Int32] = [CTL_HW, HW_MACHINE] var size: Int = 2 @@ -126,7 +162,7 @@ internal class WatchVendor: Vendor { let screenSize = device.screenBounds.size return ScreenSize(width: Double(screenSize.width), height: Double(screenSize.height), density: device.screenScale) } - + override var connection: ConnectionStatus { let path = NWPathMonitor().currentPath let interfaces = path.availableInterfaces @@ -161,7 +197,7 @@ internal class WatchVendor: Vendor { let model = String(cString: hw_machine) return model } - + } #endif @@ -213,7 +249,7 @@ internal class MacVendor: Vendor { let screenSize = NSScreen.main?.frame.size ?? CGSize(width: 0, height: 0) return ScreenSize(width: Double(screenSize.width), height: Double(screenSize.height), density: Double(NSScreen.main?.backingScaleFactor ?? 0)) } - + override var connection: ConnectionStatus { return connectionStatus() } @@ -232,34 +268,34 @@ internal class MacVendor: Vendor { } return identifier } - + private func macAddress(bsd: String) -> String? { let MAC_ADDRESS_LENGTH = 6 let separator = ":" - + var length: size_t = 0 var buffer: [CChar] - + let bsdIndex = Int32(if_nametoindex(bsd)) if bsdIndex == 0 { return nil } let bsdData = Data(bsd.utf8) var managementInfoBase = [CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, bsdIndex] - + if sysctl(&managementInfoBase, 6, nil, &length, nil, 0) < 0 { return nil } - + buffer = [CChar](unsafeUninitializedCapacity: length, initializingWith: {buffer, initializedCount in for x in 0...stride + 1 let rangeOfToken = infoData[indexAfterMsghdr...].range(of: bsdData)! @@ -285,20 +321,20 @@ extension ConnectionStatus { init(reachabilityFlags flags: SCNetworkReachabilityFlags) { let connectionRequired = flags.contains(.connectionRequired) let isReachable = flags.contains(.reachable) - #if !os(macOS) +#if !os(macOS) let isCellular = flags.contains(.isWWAN) - #endif - +#endif + if !connectionRequired && isReachable { - #if !os(macOS) +#if !os(macOS) if isCellular { self = .online(.cellular) } else { self = .online(.wifi) } - #else +#else self = .online(.wifi) - #endif +#endif } else { self = .offline @@ -310,20 +346,20 @@ internal func connectionStatus() -> ConnectionStatus { var zeroAddress = sockaddr_in() zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress)) zeroAddress.sin_family = sa_family_t(AF_INET) - + guard let defaultRouteReachability = (withUnsafePointer(to: &zeroAddress) { $0.withMemoryRebound(to: sockaddr.self, capacity: 1) { zeroSockAddress in SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress) } }) else { - return .unknown + return .unknown } - + var flags: SCNetworkReachabilityFlags = [] if !SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags) { return .unknown } - + return ConnectionStatus(reachabilityFlags: flags) } From 6d67ff13fe8b36f8482391dc499ff576e1f6f229 Mon Sep 17 00:00:00 2001 From: Desu Sai Venkat Date: Fri, 2 Jun 2023 12:12:14 +0530 Subject: [PATCH 2/4] fix: added conditional blocks for retrieverCarrierNames function --- .../Classes/Helpers/Platforms/Vendors/AppleUtils.swift | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift b/Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift index 03a0b258..2b1606df 100644 --- a/Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift +++ b/Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift @@ -12,11 +12,13 @@ import Foundation #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst) import SystemConfiguration -import CoreTelephony import UIKit #if !os(tvOS) import WebKit #endif +#if os(iOS) +import CoreTelephony +#endif internal class PhoneVendor: Vendor { private let device = UIDevice.current @@ -79,6 +81,7 @@ internal class PhoneVendor: Vendor { return retrieveCarrierNames() ?? "unavailable"; } +#if os(iOS) func retrieveCarrierNames() -> String? { if #available(iOS 16, *) { RSClient.rsLog(message: "Unable to retrieve carrier name", logLevel: .warning) @@ -88,8 +91,8 @@ internal class PhoneVendor: Vendor { var carrierNames: [String] = [] if let carriers = networkInfo.serviceSubscriberCellularProviders?.values { - for carrier in carriers { - if let carrierName = carrier.carrierName, carrierName != "--" { + for carrierObj in carriers { + if let carrierName = carrierObj.carrierName, carrierName != "--" { carrierNames.append(carrierName) } } @@ -106,6 +109,7 @@ internal class PhoneVendor: Vendor { } return nil } +#endif private func deviceModel() -> String { var name: [Int32] = [CTL_HW, HW_MACHINE] From f735aece7ba35535e7a37b37c0af43cc0b5b2348 Mon Sep 17 00:00:00 2001 From: Desu Sai Venkat Date: Fri, 2 Jun 2023 13:40:11 +0530 Subject: [PATCH 3/4] fix: minor fixes --- Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift b/Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift index 2b1606df..745c417b 100644 --- a/Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift +++ b/Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift @@ -75,10 +75,12 @@ internal class PhoneVendor: Vendor { } override var carrier: String { -#if !os(iOS) +#if os(iOS) + return retrieveCarrierNames() ?? "unavailable"; +#else return "unavailable" #endif - return retrieveCarrierNames() ?? "unavailable"; + } #if os(iOS) From 4f40028c24ef3ac6e948bfe1bd5d1b767d8d7722 Mon Sep 17 00:00:00 2001 From: Desu Sai Venkat Date: Fri, 2 Jun 2023 15:49:57 +0530 Subject: [PATCH 4/4] fix: deducing the version directly and using --- .../Platforms/Vendors/AppleUtils.swift | 44 +++++++++++-------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift b/Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift index 745c417b..ae15a9e9 100644 --- a/Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift +++ b/Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift @@ -85,28 +85,34 @@ internal class PhoneVendor: Vendor { #if os(iOS) func retrieveCarrierNames() -> String? { - if #available(iOS 16, *) { - RSClient.rsLog(message: "Unable to retrieve carrier name", logLevel: .warning) - return nil - } else if #available(iOS 12, *) { - let networkInfo = CTTelephonyNetworkInfo() - var carrierNames: [String] = [] + let systemVersion = UIDevice.current.systemVersion + let versionComponents = systemVersion.split(separator: ".").compactMap { Int($0) } + if versionComponents.count > 0 { + let majorVersion = versionComponents[0] - if let carriers = networkInfo.serviceSubscriberCellularProviders?.values { - for carrierObj in carriers { - if let carrierName = carrierObj.carrierName, carrierName != "--" { - carrierNames.append(carrierName) + if majorVersion >= 16 { + RSClient.rsLog(message: "Unable to retrieve carrier name as the iOS version is >= 16", logLevel: .warning) + return nil + } else if majorVersion >= 12 && majorVersion < 16 { + let networkInfo = CTTelephonyNetworkInfo() + var carrierNames: [String] = [] + + if let carriers = networkInfo.serviceSubscriberCellularProviders?.values { + for carrierObj in carriers { + if let carrierName = carrierObj.carrierName, carrierName != "--" { + carrierNames.append(carrierName) + } } } - } - if(!carrierNames.isEmpty) { - let formattedCarrierNames = carrierNames.joined(separator: ", ") - return formattedCarrierNames - } - } else { - let networkInfo = CTTelephonyNetworkInfo() - if let carrier = networkInfo.subscriberCellularProvider?.carrierName, carrier != "--" { - return carrier + if(!carrierNames.isEmpty) { + let formattedCarrierNames = carrierNames.joined(separator: ", ") + return formattedCarrierNames + } + } else { + let networkInfo = CTTelephonyNetworkInfo() + if let carrier = networkInfo.subscriberCellularProvider?.carrierName, carrier != "--" { + return carrier + } } } return nil