可达性和 IPv6

Reachability and IPv6

我的一个项目使用 Apple 的 Reachability class 来监控网络状态并在发生变化时收到通知。

阅读 this article 有关支持 IPv6 的内容后,我想知道是否要对此 class 进行修改以使其与 IPv6 一起使用。

我在 the same article 之后设置了一个 IPv6 网络,一切似乎都运行良好,但可能设置有问题。

Reachability 的一部分 class 是检查 Internet 连接是否与现在一样使用 IPv6 还是需要一些更改?

我对 IPv6 和可达性也有类似的疑问 class。 Apple 已在他们的文档中要求提供对 IPv6 类型的支持,如 (struct in_addr6, AF_INET6, struct sockaddr_in6 etc) 但 class 似乎尚未更新以具有这些类型。这是我的疑惑。我还没有尝试 IPv6 测试,只是在进行静态代码检查时观察到了。可能需要进行一些更改。

Reachability.m

+ (instancetype)reachabilityForInternetConnection
{
    struct sockaddr_in zeroAddress;

    bzero(&zeroAddress, sizeof(zeroAddress));

    zeroAddress.sin_len = sizeof(zeroAddress);

    zeroAddress.sin_family = AF_INET;

    return [self reachabilityWithAddress:&zeroAddress];
}

A​​pple 本身的简短回答 (https://developer.apple.com/videos/play/wwdc2015/719/ at ~10:30 - though I would recommend to watch the video in full - or at least look through at the key points here: http://www.internetsociety.org/deploy360/blog/2015/06/video-of-apple-wwdc-session-about-ipv6-and-ios-9-now-available-and-some-screenshots/):

只需尝试连接即可。

来自 https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/UnderstandingandPreparingfortheIPv6Transition/UnderstandingandPreparingfortheIPv6Transition.html#//apple_ref/doc/uid/TP40010220-CH213-SW25 的复制粘贴,重申了这一点:

Connect Without Preflight

The Reachability APIs (see SCNetworkReachability Reference) are intended for diagnostic purposes after identifying a connectivity issue. Many apps incorrectly use these APIs to proactively check for an Internet connection by calling the SCNetworkReachabilityCreateWithAddress method and passing it an IPv4 address of 0.0.0.0, which indicates that there is a router on the network. However, the presence of a router doesn’t guarantee that an Internet connection exists. In general, avoid preflighting network reachability. Just try to make a connection and gracefully handle failures. If you must check for network availability, avoid calling the SCNetworkReachabilityCreateWithAddress method. Call the SCNetworkReachabilityCreateWithName method and pass it a hostname instead.

Some apps also pass the SCNetworkReachabilityCreateWithAddress method an IPv4 address of 169.254.0.0, a self-assigned link-local address, to check for an active Wi-Fi connection. To check for Wi-Fi or cellular connectivity, look for the network reachability flag kSCNetworkReachabilityFlagsIsWWAN instead.