This is an automated email from the ASF dual-hosted git repository.

manuelbeck pushed a commit to branch master
in repository 
https://gitbox.apache.org/repos/asf/cordova-plugin-network-information.git


The following commit(s) were added to refs/heads/master by this push:
     new a76687b  fix(ios): fix deprecation warnings for iOS 12+ (#169)
a76687b is described below

commit a76687b19e442061466cdfa297250b274f7560d6
Author: Manuel Beck <[email protected]>
AuthorDate: Tue Mar 31 14:57:10 2026 +0200

    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
---
 src/ios/CDVConnection.m | 83 +++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 67 insertions(+), 16 deletions(-)

diff --git a/src/ios/CDVConnection.m b/src/ios/CDVConnection.m
index 1a52d14..fbbcf8d 100644
--- a/src/ios/CDVConnection.m
+++ b/src/ios/CDVConnection.m
@@ -60,34 +60,34 @@
                 return @"none";
             } else {
                 if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0" 
options:NSNumericSearch] != NSOrderedAscending) {
-                    CTTelephonyNetworkInfo *telephonyInfo = 
[CTTelephonyNetworkInfo new];
-                    if ([telephonyInfo.currentRadioAccessTechnology 
isEqualToString:CTRadioAccessTechnologyGPRS]) {
+                    NSString* radioAccessTechnology = [self 
currentRadioAccessTechnology];
+                    if ([radioAccessTechnology 
isEqualToString:CTRadioAccessTechnologyGPRS]) {
                         return @"2g";
-                    } else if ([telephonyInfo.currentRadioAccessTechnology  
isEqualToString:CTRadioAccessTechnologyEdge]) {
+                    } else if ([radioAccessTechnology 
isEqualToString:CTRadioAccessTechnologyEdge]) {
                         return @"2g";
-                    } else if ([telephonyInfo.currentRadioAccessTechnology  
isEqualToString:CTRadioAccessTechnologyWCDMA]) {
+                    } else if ([radioAccessTechnology 
isEqualToString:CTRadioAccessTechnologyWCDMA]) {
                         return @"3g";
-                    } else if ([telephonyInfo.currentRadioAccessTechnology  
isEqualToString:CTRadioAccessTechnologyHSDPA]) {
+                    } else if ([radioAccessTechnology 
isEqualToString:CTRadioAccessTechnologyHSDPA]) {
                         return @"3g";
-                    } else if ([telephonyInfo.currentRadioAccessTechnology  
isEqualToString:CTRadioAccessTechnologyHSUPA]) {
+                    } else if ([radioAccessTechnology 
isEqualToString:CTRadioAccessTechnologyHSUPA]) {
                         return @"3g";
-                    } else if ([telephonyInfo.currentRadioAccessTechnology  
isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
+                    } else if ([radioAccessTechnology 
isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
                         return @"3g";
-                    } else if ([telephonyInfo.currentRadioAccessTechnology  
isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) {
+                    } else if ([radioAccessTechnology 
isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) {
                         return @"3g";
-                    } else if ([telephonyInfo.currentRadioAccessTechnology  
isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) {
+                    } else if ([radioAccessTechnology 
isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) {
                         return @"3g";
-                    } else if ([telephonyInfo.currentRadioAccessTechnology  
isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
+                    } else if ([radioAccessTechnology 
isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
                         return @"3g";
-                    } else if ([telephonyInfo.currentRadioAccessTechnology  
isEqualToString:CTRadioAccessTechnologyeHRPD]) {
+                    } else if ([radioAccessTechnology 
isEqualToString:CTRadioAccessTechnologyeHRPD]) {
                         return @"3g";
-                    } else if ([telephonyInfo.currentRadioAccessTechnology  
isEqualToString:CTRadioAccessTechnologyLTE]) {
+                    } else if ([radioAccessTechnology 
isEqualToString:CTRadioAccessTechnologyLTE]) {
                         return @"4g";
                         // 5G available since iOS 14.1
                     } else if (@available(iOS 14.1, *)) {
-                        if ([telephonyInfo.currentRadioAccessTechnology 
isEqualToString:CTRadioAccessTechnologyNRNSA]) {
+                        if ([radioAccessTechnology 
isEqualToString:CTRadioAccessTechnologyNRNSA]) {
                             return @"5g";
-                        } else if ([telephonyInfo.currentRadioAccessTechnology 
isEqualToString:CTRadioAccessTechnologyNR]) {
+                        } else if ([radioAccessTechnology 
isEqualToString:CTRadioAccessTechnologyNR]) {
                             return @"5g";
                         }
                     }
@@ -109,6 +109,47 @@
     }
 }
 
+- (NSString*)currentRadioAccessTechnology
+{
+    CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new];
+
+    if (@available(iOS 12.0, *)) {
+        // iOS 12+ exposes Radio Access Technology per cellular service 
(multi-SIM capable)
+        NSDictionary<NSString*, NSString*> 
*serviceCurrentRadioAccessTechnology = 
telephonyInfo.serviceCurrentRadioAccessTechnology;
+        if (serviceCurrentRadioAccessTechnology.count == 0) {
+            return nil;
+        }
+
+        NSString *currentRadioAccessTechnology = nil;
+#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
+        if (@available(iOS 13.0, *)) {
+            // Prefer the Radio Access Technology for the currently active 
data service when available.
+            // This is the most likely to reflect the actual network type used 
for data connectivity.
+            NSString *dataServiceIdentifier = 
telephonyInfo.dataServiceIdentifier;
+
+            if (dataServiceIdentifier.length > 0) {
+                currentRadioAccessTechnology = 
serviceCurrentRadioAccessTechnology[dataServiceIdentifier];
+            }
+        }
+#endif
+        if (!currentRadioAccessTechnology) {
+            // Deterministic fallback for iOS 12 (or missing data service 
identifier)
+            // Get the Radio Access Technology for the first available 
cellular service, sorted by service identifier.
+            NSArray<NSString*> *sortedKeys = 
[[serviceCurrentRadioAccessTechnology allKeys] 
sortedArrayUsingSelector:@selector(compare:)];
+            NSString *firstKey = sortedKeys.firstObject;
+            currentRadioAccessTechnology = firstKey != nil ? 
serviceCurrentRadioAccessTechnology[firstKey] : nil;
+        }
+
+        return currentRadioAccessTechnology;
+    }
+
+// Fallback for iOS 11 and earlier, which only supports a single Radio Access 
Technology.
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+    return telephonyInfo.currentRadioAccessTechnology;
+#pragma clang diagnostic pop
+}
+
 - (BOOL)isCellularConnection:(NSString*)theConnectionType
 {
     return [theConnectionType isEqualToString:@"2g"] ||
@@ -160,8 +201,18 @@
     [self.internetReach startNotifier];
     [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(updateConnectionType:)
                                                  
name:kReachabilityChangedNotification object:nil];
-    [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(updateConnectionType:)
-                                                 
name:CTRadioAccessTechnologyDidChangeNotification object:nil];
+    if (@available(iOS 12.0, *)) {
+        // iOS 12+ replacement for 
CTRadioAccessTechnologyDidChangeNotification.
+        [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(updateConnectionType:)
+                                                     
name:CTServiceRadioAccessTechnologyDidChangeNotification object:nil];
+    } else {
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+        // iOS 11 fallback to keep backward compatibility.
+        [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(updateConnectionType:)
+                                                     
name:CTRadioAccessTechnologyDidChangeNotification object:nil];
+#pragma clang diagnostic pop
+    }
     if (UIApplicationDidEnterBackgroundNotification && 
UIApplicationWillEnterForegroundNotification) {
         [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(onPause) name:UIApplicationDidEnterBackgroundNotification 
object:nil];
         [[NSNotificationCenter defaultCenter] addObserver:self 
selector:@selector(onResume) name:UIApplicationWillEnterForegroundNotification 
object:nil];


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to