iOS Detect 3G or WiFi
Solution 1:
Using the code that Apple has provided here
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus status = [reachability currentReachabilityStatus];
if(status == NotReachable)
{
//No internet
}
else if (status == ReachableViaWiFi)
{
//WiFi
}
else if (status == ReachableViaWWAN)
{
//3G
}
Solution 2:
If you don't want to import Reachability library or deal with notifiers, you can use this simple synchronous method:
typedef enum {
ConnectionTypeUnknown,
ConnectionTypeNone,
ConnectionType3G,
ConnectionTypeWiFi
} ConnectionType;
+ (ConnectionType)connectionType
{
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, "8.8.8.8");
SCNetworkReachabilityFlags flags;
BOOL success = SCNetworkReachabilityGetFlags(reachability, &flags);
CFRelease(reachability);
if (!success) {
return ConnectionTypeUnknown;
}
BOOL isReachable = ((flags & kSCNetworkReachabilityFlagsReachable) != 0);
BOOL needsConnection = ((flags & kSCNetworkReachabilityFlagsConnectionRequired) != 0);
BOOL isNetworkReachable = (isReachable && !needsConnection);
if (!isNetworkReachable) {
return ConnectionTypeNone;
} else if ((flags & kSCNetworkReachabilityFlagsIsWWAN) != 0) {
return ConnectionType3G;
} else {
return ConnectionTypeWiFi;
}
}
Solution 3:
Import Apple's Reachability and try this,
#import "Reachability.h"
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
//Try this
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];
NetworkStatus status = [reachability currentReachabilityStatus];
if(status == NotReachable)
{
NSLog(@"none");
//No internet
}
else if (status == ReachableViaWiFi)
{
NSLog(@"Wifi");
//WiFi
}
else if (status == ReachableViaWWAN)
{
NSLog(@"WWAN");
//connection type
CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init];
_carrier = [[netinfo subscriberCellularProvider] carrierName];
if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) {
NSLog(@"2G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) {
NSLog(@"2G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) {
NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]) {
NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]) {
NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
NSLog(@"2G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) {
NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) {
NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) {
NSLog(@"3G");
} else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
NSLog(@"4G");
}
}
References(Links may broke in future) :
- iOS Detect 3G or WiFi
-
Detect carrier connection type (3G / EDGE / GPRS)
-
What are the differences between 1G, 2G, 3G, 4G and 5G?
-
CDMA2000
-
wiki 2G
What is the difference between CDMA and WCDMA network sim
What is "EV-DO Rev. A"?
Solution 4:
When using iOS 12 or newer, you can use NWPathMonitor
instead of the pre-historic Reachability
class :
import Network // Put this on top of your class
let monitor = NWPathMonitor()
monitor.pathUpdateHandler = { path in
if path.status != .satisfied {
// Not connected
}
else if path.usesInterfaceType(.cellular) {
// Cellular 3/4/5g connection
}
else if path.usesInterfaceType(.wifi) {
// Wi-fi connection
}
else if path.usesInterfaceType(.wiredEthernet) {
// Ethernet connection
}
}
monitor.start(queue: DispatchQueue.global(qos: .background))