Reachability with IP address won't work.

2019-08-09 23:18发布

After inputting the Rechability classes Apple provided. Also after inputting this, Reachability reachabilityWithAddress:(const struct sockaddr_in *)hostAddress. How should we input the IP address we want to check into this line? This is the part where i'm really lost at.

2条回答
祖国的老花朵
2楼-- · 2019-08-10 00:05

Please try the following:

if ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == NotReachable) {
        // do somehting meaningful!
}

or to be more specific:

Reachability* reachability = [Reachability sharedReachability];
[reachability setHostName:@"www.google.com"]; // set your host name/ip here
NetworkStatus remoteHostStatus = [reachability remoteHostStatus];

if (remoteHostStatus == NotReachable) { NSLog(@"no"); }
else if (remoteHostStatus == ReachableViaWiFiNetwork) { NSLog(@"wifi"); }
else if (remoteHostStatus == ReachableViaCarrierDataNetwork) { NSLog(@"cellular"); }
查看更多
Deceive 欺骗
3楼-- · 2019-08-10 00:16

struct sockaddr_in is a low-level "socket address" type used by BSD sockets. We generally don't deal with them at the Cocoa level, but they can creep up from time to time, including in Apple's demo class. The reason there is because SCNetworkReachability uses a struct sockaddr_in in one of its creation functions.

Fortunately for you, however, you can supply a string instead with the +reachabilityWithHostName: method, and that includes IP addresses (which, like hostnames, will be resolved automatically for you by the underlying network APIs.)

Reachability *r = [Reachability reachabilityWithHostName:@"1.2.3.4"];
查看更多
登录 后发表回答