输入Rechability类苹果提供后。 同时此输入后,可达性reachabilityWithAddress:(const的结构SOCKADDR_IN *)hostAddress。 我们应该如何输入IP地址,我们要检查到这条线? 这是我真的失去了一部分。
Answer 1:
struct sockaddr_in
是低电平“套接字地址”类型由BSD套接字使用。 我们一般不会在可可一级对付他们,但他们可以从时间攀升到时间,包括在苹果的演示课。 究其原因,是因为SCNetworkReachability
使用struct sockaddr_in
在其创作的功能之一。
幸运的是你,但是,你可以提供一个字符串代替+reachabilityWithHostName:
方法,这包括IP地址(其中,像主机名,将自动为您下层网络的API解决。)
Reachability *r = [Reachability reachabilityWithHostName:@"1.2.3.4"];
Answer 2:
请尝试以下方法:
if ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == NotReachable) {
// do somehting meaningful!
}
或者更具体:
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"); }
文章来源: Reachability with IP address won't work.