Skip to content

Commit a76687b

Browse files
authored
fix(ios): fix deprecation warnings for iOS 12+ (#169)
* fix(ios): fix deprecation warnings for iOS 12+ - `currentRadioAccessTechnology` is deprecated since iOS 12: https://developer.apple.com/documentation/coretelephony/cttelephonynetworkinfo/currentradioaccesstechnology?language=objc.`serviceCurrentRadioAccessTechnology` should be used. - Add method `currentRadioAccessTechnology` which uses `serviceCurrentRadioAccessTechnology` on iOS 12 and newer and `currentRadioAccessTechnology` on iOS 11 and older - `CTRadioAccessTechnologyDidChangeNotification` is deprecated since iOS 12: https://developer.apple.com/documentation/coretelephony/ctradioaccesstechnologydidchangenotification?language=objc. Instead `CTServiceRadioAccessTechnologyDidChangeNotification` should be used. * fix(ios): get active sim for mobile data on iOS 13+ - serviceCurrentRadioAccessTechnology is a dictionary (multi‑SIM). Using allValues.firstObject is non-deterministic and can report the wrong Radio Access Technology when multiple services exist. Prefer selecting the Radio Access Technology for the active data service (e.g., via dataServiceIdentifier when available) and fall back deterministically when it’s not. - Generated-By: GitHub Copilot AI
1 parent 0a86a7b commit a76687b

File tree

1 file changed

+67
-16
lines changed

1 file changed

+67
-16
lines changed

src/ios/CDVConnection.m

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,34 @@ - (NSString*)w3cConnectionTypeFor:(CDVReachability*)reachability
6060
return @"none";
6161
} else {
6262
if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" options:NSNumericSearch] != NSOrderedAscending) {
63-
CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new];
64-
if ([telephonyInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) {
63+
NSString* radioAccessTechnology = [self currentRadioAccessTechnology];
64+
if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) {
6565
return @"2g";
66-
} else if ([telephonyInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) {
66+
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) {
6767
return @"2g";
68-
} else if ([telephonyInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) {
68+
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) {
6969
return @"3g";
70-
} else if ([telephonyInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]) {
70+
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]) {
7171
return @"3g";
72-
} else if ([telephonyInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]) {
72+
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]) {
7373
return @"3g";
74-
} else if ([telephonyInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
74+
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
7575
return @"3g";
76-
} else if ([telephonyInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) {
76+
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) {
7777
return @"3g";
78-
} else if ([telephonyInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) {
78+
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) {
7979
return @"3g";
80-
} else if ([telephonyInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
80+
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
8181
return @"3g";
82-
} else if ([telephonyInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) {
82+
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) {
8383
return @"3g";
84-
} else if ([telephonyInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
84+
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
8585
return @"4g";
8686
// 5G available since iOS 14.1
8787
} else if (@available(iOS 14.1, *)) {
88-
if ([telephonyInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyNRNSA]) {
88+
if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyNRNSA]) {
8989
return @"5g";
90-
} else if ([telephonyInfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyNR]) {
90+
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyNR]) {
9191
return @"5g";
9292
}
9393
}
@@ -109,6 +109,47 @@ - (NSString*)w3cConnectionTypeFor:(CDVReachability*)reachability
109109
}
110110
}
111111

112+
- (NSString*)currentRadioAccessTechnology
113+
{
114+
CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new];
115+
116+
if (@available(iOS 12.0, *)) {
117+
// iOS 12+ exposes Radio Access Technology per cellular service (multi-SIM capable)
118+
NSDictionary<NSString*, NSString*> *serviceCurrentRadioAccessTechnology = telephonyInfo.serviceCurrentRadioAccessTechnology;
119+
if (serviceCurrentRadioAccessTechnology.count == 0) {
120+
return nil;
121+
}
122+
123+
NSString *currentRadioAccessTechnology = nil;
124+
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
125+
if (@available(iOS 13.0, *)) {
126+
// Prefer the Radio Access Technology for the currently active data service when available.
127+
// This is the most likely to reflect the actual network type used for data connectivity.
128+
NSString *dataServiceIdentifier = telephonyInfo.dataServiceIdentifier;
129+
130+
if (dataServiceIdentifier.length > 0) {
131+
currentRadioAccessTechnology = serviceCurrentRadioAccessTechnology[dataServiceIdentifier];
132+
}
133+
}
134+
#endif
135+
if (!currentRadioAccessTechnology) {
136+
// Deterministic fallback for iOS 12 (or missing data service identifier)
137+
// Get the Radio Access Technology for the first available cellular service, sorted by service identifier.
138+
NSArray<NSString*> *sortedKeys = [[serviceCurrentRadioAccessTechnology allKeys] sortedArrayUsingSelector:@selector(compare:)];
139+
NSString *firstKey = sortedKeys.firstObject;
140+
currentRadioAccessTechnology = firstKey != nil ? serviceCurrentRadioAccessTechnology[firstKey] : nil;
141+
}
142+
143+
return currentRadioAccessTechnology;
144+
}
145+
146+
// Fallback for iOS 11 and earlier, which only supports a single Radio Access Technology.
147+
#pragma clang diagnostic push
148+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
149+
return telephonyInfo.currentRadioAccessTechnology;
150+
#pragma clang diagnostic pop
151+
}
152+
112153
- (BOOL)isCellularConnection:(NSString*)theConnectionType
113154
{
114155
return [theConnectionType isEqualToString:@"2g"] ||
@@ -160,8 +201,18 @@ - (void)pluginInitialize
160201
[self.internetReach startNotifier];
161202
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateConnectionType:)
162203
name:kReachabilityChangedNotification object:nil];
163-
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateConnectionType:)
164-
name:CTRadioAccessTechnologyDidChangeNotification object:nil];
204+
if (@available(iOS 12.0, *)) {
205+
// iOS 12+ replacement for CTRadioAccessTechnologyDidChangeNotification.
206+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateConnectionType:)
207+
name:CTServiceRadioAccessTechnologyDidChangeNotification object:nil];
208+
} else {
209+
#pragma clang diagnostic push
210+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
211+
// iOS 11 fallback to keep backward compatibility.
212+
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateConnectionType:)
213+
name:CTRadioAccessTechnologyDidChangeNotification object:nil];
214+
#pragma clang diagnostic pop
215+
}
165216
if (UIApplicationDidEnterBackgroundNotification && UIApplicationWillEnterForegroundNotification) {
166217
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onPause) name:UIApplicationDidEnterBackgroundNotification object:nil];
167218
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onResume) name:UIApplicationWillEnterForegroundNotification object:nil];

0 commit comments

Comments
 (0)