1

输入 Apple 提供的 Rechability 类后。同样在输入后,ReachabilityreachabilityWithAddress:(const struct sockaddr_in *)hostAddress。我们应该如何在这一行输入我们要检查的 IP 地址?这是我真正迷失的部分。

4

2 回答 2

2

struct sockaddr_in是 BSD 套接字使用的低级“套接字地址”类型。我们通常不会在 Cocoa 级别处理它们,但它们会不时出现,包括在 Apple 的演示类中。原因是因为在其创建功能之一中SCNetworkReachability使用了 a 。struct sockaddr_in

然而,幸运的是,您可以在+reachabilityWithHostName:方法中提供一个字符串,其中包括 IP 地址(与主机名一样,底层网络 API 将自动为您解析这些地址。)

Reachability *r = [Reachability reachabilityWithHostName:@"1.2.3.4"];
于 2012-08-29T12:57:35.643 回答
0

请尝试以下方法:

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"); }
于 2012-08-29T12:53:14.757 回答