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

user3549189

import Foundation import SystemConfiguration

public class Reachability {

class func isConnectedToNetwork() -> Bool {

    var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
    zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
    zeroAddress.sin_family = sa_family_t(AF_INET)

    let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
        SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, UnsafePointer($0))
    }

    var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
    if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
        return false
    }

    let isReachable = flags == .Reachable
    let needsConnection = flags == .ConnectionRequired

    return isReachable && !needsConnection

}

}

I am using the above written code for checking internet connection for my app, this checks only 3G and WIFI connections. But I need to check for 4G Connections also. Can anyone help me to find the solution.

ovo

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Detect if internet connection is available (WIFI and 4G) on SWIFT 3

Android google maps v2 not working on mobile internet(3g or 4g) but its working on wifi

How do mobile phone companies detect if you are sharing your 3G/4G internet connection?

How to switch networks(2G/3G/4G) WITHOUT jailbreak in iOS7?

How to check if WiFi is on or off in iOS Swift 2?

Prevent Wifi AP from Blocking 3g/4g Internet

Android 127.0.0.1 (loopback) connection: Works on WiFi, fails with 3G/4G

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

How detect networktype (2G/3G/LTE) when connected to WiFi

Check for internet connection with Swift

Mapping iOS 7 constants to 2G, 3G, 4G, LTE etc

How to check if my app is using WIFI or 3G service

Determine 2G or 3G connection

Check for internet connection availability in Swift

Is there a way to check the Internet connection quality in React Native (i.e. 3g vs LTE)?

How to check if device is having poor internet connection in swift

How can I see the signal strength and type of connection (2G/3G) using a usb modem in Linux?

How can i configure my network so i can access a linux box behind 2 routers (4G and wifi)

How to make my laptop a WiFi Hotspot (using internet from USB 3G device)

Check if Internet is available including 4G

Swift iOS- What to do when there is a wifi connection but no internet connection?

How to detect if user has 3G/Wifi on before making URL connection?

How check WatchOS 2 connected (reachability) to the internet?

How to share the 3g internet connection of my Android Nexus One with my laptop?

How to check host connection with internet connection

Android - Check internet connection through an specific network (WIFI)

How to check if Wifi network has internet access?

Phonegap android app in 3g or 2g connection index.html loading very slow

Internet connection drops out after 1-2 hours (RTL8821AE wifi drivers)