ReachabilityWithAddress error giving it an ip address

允我心安 提交于 2019-12-06 07:14:08

I tested this sample in Reachability sample provided by apple, hope you get the idea.

//Change the host name here to change the server your monitoring
remoteHostLabel.text = [NSString stringWithFormat: @"Remote Host: %@", @"www.apple.com"];
//commented this line in the applicationDidFinishLaunching of ReachabilityAppDelegate.m file
//hostReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
 struct sockaddr_in tAddr;
tAddr.sin_len = 16;
tAddr.sin_port = htons(80);
struct in_addr  address;
address.s_addr = htons(0x4a7de048);
tAddr.sin_family = AF_INET;
 //http://74.125.224.72/ this ip adress is for google
 //4a7de048 **updated** Hexadecimal representation of IP address 74.125.224.72

hostReach = [[Reachability reachabilityWithAddress:&tAddr] retain];

This code works for me:

    struct sockaddr_in localWifiAddress;
    bzero(&localWifiAddress, sizeof(localWifiAddress));
    localWifiAddress.sin_len = sizeof(localWifiAddress);
    localWifiAddress.sin_family = AF_INET;
    localWifiAddress.sin_addr.s_addr = htonl(0x0A0A0A7B); // hex representation of your local IP

    Reachability *reachability = [Reachability reachabilityWithAddress:&localWifiAddress];    
    reachability.key = kLocalWiFiConnection;
    return [reachability isReachable];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!