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

feat: added support for sending carrier names on iOS versions < 16 instead of sending un-available #330

Merged
merged 4 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
98 changes: 73 additions & 25 deletions Sources/Classes/Helpers/Platforms/Vendors/AppleUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import UIKit
#if !os(tvOS)
import WebKit
#endif
#if os(iOS)
import CoreTelephony
#endif

internal class PhoneVendor: Vendor {
private let device = UIDevice.current
Expand All @@ -25,15 +28,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 {
Expand Down Expand Up @@ -71,6 +74,51 @@ internal class PhoneVendor: Vendor {
return [RSiOSLifecycleMonitor(), RSDeviceTokenPlugin()]
}

override var carrier: String {
#if os(iOS)
return retrieveCarrierNames() ?? "unavailable";
#else
return "unavailable"
#endif

}

#if os(iOS)
func retrieveCarrierNames() -> String? {
let systemVersion = UIDevice.current.systemVersion
let versionComponents = systemVersion.split(separator: ".").compactMap { Int($0) }
if versionComponents.count > 0 {
let majorVersion = versionComponents[0]

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
}
}
}
return nil
}
#endif

private func deviceModel() -> String {
var name: [Int32] = [CTL_HW, HW_MACHINE]
var size: Int = 2
Expand Down Expand Up @@ -126,7 +174,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
Expand Down Expand Up @@ -161,7 +209,7 @@ internal class WatchVendor: Vendor {
let model = String(cString: hw_machine)
return model
}

}

#endif
Expand Down Expand Up @@ -213,7 +261,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()
}
Expand All @@ -232,34 +280,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..<length { buffer[x] = 0 }
initializedCount = length
})

if sysctl(&managementInfoBase, 6, &buffer, &length, nil, 0) < 0 {
return nil
}

let infoData = Data(bytes: buffer, count: length)
let indexAfterMsghdr = MemoryLayout<if_msghdr>.stride + 1
let rangeOfToken = infoData[indexAfterMsghdr...].range(of: bsdData)!
Expand All @@ -285,20 +333,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
Expand All @@ -310,20 +358,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)
}

Expand Down