Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check VPN Connectivity in iPhone

Tags:

ios

vpn

I am new to this development. I searched a lot over Internet but cant find any proper solution for it. I want to check VPN connectivity available in my iPhone application as I am fetching data from client machine which is connected through VPN. I did check if Internet connection is available but unable to check if VPN is connected or not. Please suggest me on this.

like image 317
P.J Avatar asked Nov 15 '11 05:11

P.J


1 Answers

I've just found another way to check VPN connection, which wasn't mentioned anywhere. You can use getifaddrs() to get current network interfaces. Then iterate though them and check their names. If any of them contains tap,tun or ppp then user has connected VPN.

  - (BOOL)isVPNConnected
{
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;

    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0) {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while (temp_addr != NULL) {
            NSString *string = [NSString stringWithFormat:@"%s" , temp_addr->ifa_name];
            if ([string rangeOfString:@"tap"].location != NSNotFound ||
                [string rangeOfString:@"tun"].location != NSNotFound ||
                [string rangeOfString:@"ppp"].location != NSNotFound){
                return YES;
            }

            temp_addr = temp_addr->ifa_next;
        }
    }

    // Free memory
    freeifaddrs(interfaces);
    return NO;
}

UPD: on iOS 9 code above is not working. Interfaces are the same, no matter if VPN is connected or not. Try following code in this case:

 - (BOOL)isVPNConnected
{
    NSDictionary *dict = CFBridgingRelease(CFNetworkCopySystemProxySettings());
        NSArray *keys = [dict[@"__SCOPED__"]allKeys];
        for (NSString *key in keys) {
            if ([key rangeOfString:@"tap"].location != NSNotFound ||
                [key rangeOfString:@"tun"].location != NSNotFound ||
                [key rangeOfString:@"ppp"].location != NSNotFound){
                return YES;
            }
        }
        return NO;
}
like image 160
Timur Suleimanov Avatar answered Dec 22 '22 20:12

Timur Suleimanov