IOS 检测3G 或 WiFi

我不确定这是否可能,但我有这种情况。

我有一个网站显示在我的 UIWebView,它的链接设置在一个 UISegmentedController。 他们的网站可以检测,如果你是在 wifi 或在3g 网络。

现在分段控制器指向两个不同的页面: 1-一个 iPhone 友好的登录界面 2-主页,一旦你登录。

现在的问题是:

我可以编程我的应用程序来检测它是否是 WIFI 或3G (我知道你可以做到这一点) ,但然后根据答案去区段1或2

就像这样:

if (iPhone device is on 3g) {
Go to Segment 1;
} else {
Go to Segment 0;
}
92234 次浏览

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
}

If you are using Xamarin or Monotouch you can use Reachability adapted class from Xamarin GitHub repository:

https://github.com/xamarin/monotouch-samples/blob/master/ReachabilitySample/reachability.cs

So add it to your project and call Reachability.InternetConnectionStatus()

I made a pretty simple block based Reachability wrapper that strips all the outdated C-like Reachability code, poured into a much more Cocoa form.

Usage like:

[EPPZReachability reachHost:hostNameOrIPaddress
completition:^(EPPZReachability *reachability)
{
if (reachability.reachableViaCellular) [self doSomeLightweightStuff];
}];

See Reachability with blocks for everyday use at eppz!blog, or grab it directly from eppz!reachability at GitHub.

It also works with IP addresses, which turned out to be a pretty rare Reachability wrapper feature.

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;
}
}

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) :

For swift we can use:

func getNetworkType()->String {
do{
let reachability:Reachability = try Reachability.reachabilityForInternetConnection()
do{
try reachability.startNotifier()
let status = reachability.currentReachabilityStatus
if(status == .NotReachable){
return ""
}else if (status == .ReachableViaWiFi){
return "Wifi"
}else if (status == .ReachableViaWWAN){
let networkInfo = CTTelephonyNetworkInfo()
let carrierType = networkInfo.currentRadioAccessTechnology
switch carrierType{
case CTRadioAccessTechnologyGPRS?,CTRadioAccessTechnologyEdge?,CTRadioAccessTechnologyCDMA1x?: return "2G"
case CTRadioAccessTechnologyWCDMA?,CTRadioAccessTechnologyHSDPA?,CTRadioAccessTechnologyHSUPA?,CTRadioAccessTechnologyCDMAEVDORev0?,CTRadioAccessTechnologyCDMAEVDORevA?,CTRadioAccessTechnologyCDMAEVDORevB?,CTRadioAccessTechnologyeHRPD?: return "3G"
case CTRadioAccessTechnologyLTE?: return "4G"
default: return ""
}


// Get carrier name


}else{
return ""
}
}catch{
return ""
}


}catch{
return ""
}




}
#import <ifaddrs.h>
#import <arpa/inet.h>


BOOL CheckWiFi() {


struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;


BOOL hasWifi = NO;


int err = getifaddrs(&interfaces);
if(err == 0) {


temp_addr = interfaces;


while(temp_addr) {


if(temp_addr->ifa_addr->sa_family == AF_INET) {


struct sockaddr_in *addr = (struct sockaddr_in *)temp_addr->ifa_addr;


if(memcmp(temp_addr->ifa_name, "en", 2) == 0) {
hasWifi = YES;
break;
}
}


temp_addr = temp_addr->ifa_next;
}
}


freeifaddrs(interfaces);
return hasWifi;
}

To check if you are in a wifi, this saves the costly check of making a connection. Check for ifa_name "bridge" to check for internet sharing.

Class method is as follow

+(NSString*)connectedNetworkType {
Reachability *reachability = [Reachability reachabilityForInternetConnection];
[reachability startNotifier];


NetworkStatus status = [reachability currentReachabilityStatus];


if(status == NotReachable) {
NSLog(@"none");
//No internet
}
else if (status == ReachableViaWiFi) {
NSLog(@"Wifi");
//WiFi
return @"Wifi";
}
else if (status == ReachableViaWWAN){
NSLog(@"WWAN");
//connection type
CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init];
//    _carrier = [[netinfo subscriberCellularProvider] carrierName];
if (([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS])
||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge])
||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x])) {
NSLog(@"2G");
return @"2G";
}
else if (([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA])
||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA])
||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA])
||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0])
||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA])
||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB])
||([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD])){
NSLog(@"3G");
return @"3G";
}
else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
NSLog(@"4G");
return @"4G";


}
}
return @"-1";//default unknown
}

When using iOS 12 or newer, you can use NWPathMonitor instead of the pre-historic Reachabilityclass :

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))

If you don't want to use Reachability or using Swift simple answer is here

https://stackoverflow.com/a/63837522/6342609

Swift version with 5G support; based on several answers.

import Foundation
import SystemConfiguration
import CoreTelephony


/// Returns current network connection type(e.g. 4G, WIFI). It may take some time; so there is the suggestion not to use it on the main queue.
static func connectionType() -> ConnectionType {
guard let reachability = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "www.google.com") else {
return .noConnection
}
    

var flags = SCNetworkReachabilityFlags()
SCNetworkReachabilityGetFlags(reachability, &flags)
    

guard flags.contains(.reachable) else { return .noConnection }
guard flags.contains(.isWWAN) else { return .wifi }
    

let networkInfo = CTTelephonyNetworkInfo()
let carrierTypeName: String?
    

if #available(iOS 12.0, *) {
carrierTypeName = networkInfo.serviceCurrentRadioAccessTechnology?.first?.value
} else {
carrierTypeName = networkInfo.currentRadioAccessTechnology
}
    

if #available(iOS 14.0, *) {
switch carrierTypeName {
case CTRadioAccessTechnologyNR, CTRadioAccessTechnologyNRNSA:
return .cellular5G
default:
break
}
}
    

switch carrierTypeName {
case CTRadioAccessTechnologyGPRS, CTRadioAccessTechnologyEdge, CTRadioAccessTechnologyCDMA1x:
return .cellular2G
case CTRadioAccessTechnologyLTE:
return .cellular4G
case nil:
return .unknown
default:
return .cellular3G
}
}


enum ConnectionType: CustomStringConvertible {
case noConnection, unknown, wifi, cellular2G, cellular3G, cellular4G, cellular5G
        

var description: String {
switch self {
case .noConnection:
return "no connection"
case .unknown:
return "unknown"
case .wifi:
return "WIFI"
case .cellular2G:
return "2G"
case .cellular3G:
return "3G"
case .cellular4G:
return "LTE"
case .cellular5G:
return "5G"
}
}
}