forked from hideokamoto/stripe-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Send network_type as metadata on analytics requests (stripe#2720)
* Send network_type as metadata on analytics requests * Format * Update test * Fix test
- Loading branch information
1 parent
c2a3aee
commit 090bc51
Showing
4 changed files
with
58 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
StripeCore/StripeCore/Source/Analytics/NetworkDetector.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// NetworkDetector.swift | ||
// StripeCore | ||
// | ||
// Created by Nick Porter on 7/5/23. | ||
// | ||
|
||
import CoreTelephony | ||
import Foundation | ||
import SystemConfiguration | ||
|
||
/// A class which can detect the current network type of the device | ||
class NetworkDetector { | ||
|
||
static func getConnectionType() -> String? { | ||
guard let reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "www.stripe.com") else { | ||
return nil | ||
} | ||
|
||
var flags = SCNetworkReachabilityFlags() | ||
SCNetworkReachabilityGetFlags(reachability, &flags) | ||
|
||
let isReachable = flags.contains(.reachable) | ||
let isWWAN = flags.contains(.isWWAN) | ||
|
||
guard isReachable else { | ||
return nil | ||
} | ||
|
||
guard isWWAN else { | ||
return "Wi-Fi" | ||
} | ||
|
||
let networkInfo = CTTelephonyNetworkInfo() | ||
let carrierType = networkInfo.serviceCurrentRadioAccessTechnology | ||
|
||
guard let carrierTypeName = carrierType?.first?.value else { | ||
return "unknown" | ||
} | ||
|
||
switch carrierTypeName { | ||
case CTRadioAccessTechnologyGPRS, CTRadioAccessTechnologyEdge, CTRadioAccessTechnologyCDMA1x: | ||
return "2G" | ||
case CTRadioAccessTechnologyWCDMA, CTRadioAccessTechnologyHSDPA, CTRadioAccessTechnologyHSUPA, CTRadioAccessTechnologyCDMAEVDORev0, CTRadioAccessTechnologyCDMAEVDORevA, CTRadioAccessTechnologyCDMAEVDORevB, CTRadioAccessTechnologyeHRPD: | ||
return "3G" | ||
case CTRadioAccessTechnologyLTE: | ||
return "4G" | ||
default: | ||
return "5G" | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters