Ios Detect 3G or Wifi

iOS Detect 3G or WiFi

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
}

How do you detect 3G vs. Wifi connection on mobile safari?

Not unless browsers start giving this information away through the DOM which, currently, they don't. There is a navigator javascript objects which holds many things but not what you're looking for. I've also read in the news recently that some cellular carrier was adding cookies to HTTP requests made on their phones. They were found to be adding customer IDs to cookies (huge security breach!!).

Mobile Safari does add some new DOM additions but not what you're asking for.

Short of these options, you can't. Layers of the Internet Protocol are meant to encapsulate & hide the details of the bottom layers. You can't detect Edge, 3G or Wifi any more than you can detect cable, DSL or finer optics.

Finally, even if you could get this information, it wouldn't do you any good unless you had details of every single node in your TCP connection. For example, I could have the following setup :

iPad  ---WiFi---->  iPhone's Hotspot  ---3G--->  Carrier ---unknown--->>>


Addendum

In 2012-2013, the W3C was fleshing out The Network Information API which was aimed at providing "an interface for web applications to access the underlying connection information of the device". At the time, the API stipulated that an "estimated" bandwidth for the browser would be obtainable via navigator.connection.bandwidth. As of April 2014, that work has since been discontinued!!

Update 1: As of 20th October 2015, work continues on this API. You can find the latest Editor's drafts for the Network Information API on the W3C's github pages.

Update 2: In June 2020, Apple declined to implement the Network Information API in Safari due to privacy concerns

How to check 3G,4G and wifi internet connection in swift 2.2

Here is the Reachability from Apple, you need to download and drag Reachability.h/.m to your project.

Then import CoreTelephony and try below.

    if let reachability = Reachability.forInternetConnection() {
reachability.startNotifier()
let status = reachability.currentReachabilityStatus()
if status == .init(0) {
// .NotReachable
print("Not Reachable")
}
else if status == .init(1) {
// .ReachableViaWiFi
print("Reachable Via WiFi")

}
else if status == .init(2) {
// .ReachableViaWWAN
let netInfo = CTTelephonyNetworkInfo()
if let cRAT = netInfo.currentRadioAccessTechnology {
switch cRAT {
case CTRadioAccessTechnologyGPRS,
CTRadioAccessTechnologyEdge,
CTRadioAccessTechnologyCDMA1x:
print("Reachable Via 2G")
case CTRadioAccessTechnologyWCDMA,
CTRadioAccessTechnologyHSDPA,
CTRadioAccessTechnologyHSUPA,
CTRadioAccessTechnologyCDMAEVDORev0,
CTRadioAccessTechnologyCDMAEVDORevA,
CTRadioAccessTechnologyCDMAEVDORevB,
CTRadioAccessTechnologyeHRPD:
print("Reachable Via 3G")
case CTRadioAccessTechnologyLTE:
print("Reachable Via 4G")
default:
fatalError("error")
}
}
}
}

How to detect wheter running on 3G or Wi-Fi on iPhone?

Since than 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.

Sample Image

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.

Is it possible to check if WiFi is connected but there is no internet connection in Swift using Reachability?

I found a solution.

As Augie suggested, its not possible to do it with Reachability as it just checks if the WiFi or Cellular is reachable or not.

So I had to ping a website to check if it is online or not.

If I get a 200 response from it then its connected.

But if I don't get a 200, then even though WiFi is connected but there is no internet.

I didn't know how to Ping properly so I asked another question where I got the answer

The complete code can be found at https://github.com/deadcoder0904/net-alert

Detect carrier connection type (3G / EDGE / GPRS)

From iOS 7 on you can use:

CTTelephonyNetworkInfo *telephonyInfo = [CTTelephonyNetworkInfo new];
NSLog(@"Current Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology);
[NSNotificationCenter.defaultCenter addObserverForName:CTRadioAccessTechnologyDidChangeNotification
object:nil
queue:nil
usingBlock:^(NSNotification *note)
{
NSLog(@"New Radio Access Technology: %@", telephonyInfo.currentRadioAccessTechnology);
}];

I have also found this to detect a slow or fast connection:

- (BOOL)isFast:(NSString*)radioAccessTechnology {
if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) {
return NO;
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) {
return NO;
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) {
return YES;
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]) {
return YES;
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]) {
return YES;
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
return NO;
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) {
return YES;
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) {
return YES;
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
return YES;
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) {
return YES;
} else if ([radioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
return YES;
}

return YES;
}


Related Topics



Leave a reply



Submit