iOS Question Class to get internet connection availability and type

hatzisn

Well-Known Member
Licensed User
Longtime User
Hi everybody,

While waiting for a possible solution in the issue I am facing with b4i-bridge in which I cannot connect as seen in the previous post to save me some time I tried to create the following class. Could someone with more knowledge in Objective C correct the code of this class I created by sewing different examples so that it works and return if the internet is connected and what type of connection there is... (I tried to compile it with hosted builder and I get an error about reachability.h as to compile it there is no need for b4i-bridge - by the way any help on the b4i-bridge issue will be highly appreciated).

Original code can be found in:
https://stackoverflow.com/questions/7938650/ios-detect-3g-or-wifi

Code of class:

B4X:
Sub Class_Globals
 
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
 
End Sub

Public Sub GetConnectionState() As String
    Dim no As NativeObject
    no = Me
    Return no.RunMethod("getConn", Null)
End Sub

#IF OBJC


- (NSString *) getConn: {

    #import "Reachability.h"
    #import <CoreTelephony/CTTelephonyNetworkInfo.h>

    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    [reachability startNotifier];

    NetworkStatus status = [reachability currentReachabilityStatus];

    if(status == NotReachable)
    {
       return @"none";
        //No internet
    }
    else if (status == ReachableViaWiFi)
    {
        return @"Wifi";
        //WiFi
    }
    else if (status == ReachableViaWWAN)
    {

    //connection type
    CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init];
    _carrier = [[netinfo subscriberCellularProvider] carrierName];

    if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyGPRS]) {
        return @"2G";
    } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyEdge]) {
        return @"2G";
    } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyWCDMA]) {
        return @"3G";
    } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSDPA]) {
        return @"3G";
    } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyHSUPA]) {
        return @"3G";
    } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMA1x]) {
        return @"2G";
    } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORev0]) {
        return @"3G";
    } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevA]) {
        return @"3G";
    } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyCDMAEVDORevB]) {
        return @"3G";
    } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyeHRPD]) {
        return @"3G";
    } else if ([netinfo.currentRadioAccessTechnology isEqualToString:CTRadioAccessTechnologyLTE]) {
        return @"4G";
    }

    }

}

#End If
 
Last edited:
Top