0

Steps I followed 1) Added system configuration frame work and imported #import "systemconfiguration/scnetworkreachability.h" and then added below code for checking connectivity

BOOL hasLeadingNumberInString(NSString* s) {
  if(s)
      return [s length] && isnumber([s characterAtIndex:0]);
else
    return NO;
}

-(BOOL)networkConnected: (NSString *)ipAdress  {
    SCNetworkReachabilityFlags  flags = 0;
    SCNetworkReachabilityRef    netReachability;
    BOOL                        retrievedFlags = NO;

    // added the "if" and first part of if statement
    //
    if (hasLeadingNumberInString(ipAdress)) {
        struct sockaddr_in the_addr;
        memset((void *)&the_addr, 0, sizeof(the_addr));
        the_addr.sin_family = AF_INET;
        the_addr.sin_len = sizeof(the_addr);
        const char* server_addr = [ipAdress UTF8String];
        unsigned long ip_addr = inet_addr(server_addr);
        the_addr.sin_addr.s_addr = ip_addr;
        netReachability =    SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr*)&the_addr);
    } else {
        netReachability = SCNetworkReachabilityCreateWithName(NULL, [ipAdress UTF8String]);
    }
    if (netReachability) {
        retrievedFlags = SCNetworkReachabilityGetFlags(netReachability, &flags);
        CFRelease(netReachability);
    }
    if (!retrievedFlags || !flags) {
        return NO;
    }
    return YES;
}

- (void)checkconnectivity
{
    if([self networkConnected:@"192.168.2.10" ])
        NSLog(@"Connected");
    else
         NSLog(@"Not Connected");

}

For god sake it always returns connected.

method 2 i followed, I added AFnetworking using cocopods.I imported "AFNetworkReachabilityManager.h" file and used managerforaddress, it always returns unknown

 AFNetworkReachabilityManager *addressCheck=[AFNetworkReachabilityManager managerForAddress:@"192.168.3.10"];
   [addressCheck startMonitoring];
    [addressCheck setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
    }];

    NSLog(@"%hhd",addressCheck.reachable);
    NSLog(@"%hhd",[addressCheck isReachable]);

    NSString *str=[[NSString alloc]initWithString:[addressCheck localizedNetworkReachabilityStatusString]];
    NSLog(@"%@",str);

    NSLog(@"%@",addressCheck.localizedNetworkReachabilityStatusString);

}

Even in this way I am unable to get connectivity for particular address. Is there any way to get this or it's not possible.

Cœur
  • 37,241
  • 25
  • 195
  • 267
MacDeveloper
  • 1,334
  • 3
  • 16
  • 49

1 Answers1

0

You could just simply use a Process and the ping command to check if an ip address is reachable.

Swift 4 example: (macOS only)

func ping(at ipAddress: String) -> Int32 {
    let process = Process.launchedProcess(launchPath: "/sbin/ping", arguments: ["-c1", ipAddress])
    process.waitUntilExit()
    return process.terminationStatus
}

let reachable = ping(at: "10.0.1.119") == 0
Alejandro Cotilla
  • 2,501
  • 1
  • 20
  • 35