参考链接:
苹果新闻:Supporting IPv6-only Networks
适配参考:Supporting IPv6 DNS64/NAT64 Networks
苹果的Reachability:About Reachability
适配的工作多集中在 Reachability 和 SCNetworkReachability 两个地方.疑问也大多出自于此;
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 flagkSCNetworkReachabilityFlagsIsWWAN
instead.
根据上文所述,苹果应该是禁止使用
SCNetworkReachabilityCreateWithAddress
方法,而是应该用SCNetworkReachabilityCreateWithName
代替,但是再看苹果官方的 Reachability 最新适配 IPv6 的版本发现,依然在使用 SCNetworkReachabilityCreateWithAddress.
而且已经适配了 IPv6 的 AFNetWorking 也使用了此方法,所以至少可以确定,使用SCNetworkReachabilityCreateWithAddress是不会存在问题,如下图,参考Reachability 与 AF,至少使用 sockaddr_in
结合 SCNetworkReachabilityCreateWithAddress 是不会有问题的。我猜测苹果所说可能是禁止使用 169.254.0.0 这样的 ip 地址吧.
此处有个疑问是为什么AF做两部判断,而 Reachability 不做判断,这个问题在 issue 中也可以找到,下面自己可能需要对不同设备不同版本进行测试,确认下.
总结下我最终适配方案:
AF首先更新到最新;
Reachability 更换为苹果最新的适配 IPv6 的版本;
判断网络使用 Reachability 中的 reachabilityWithHostName 方法;
此方法我是针对非 iOS9 和 iOS8 的设备在 IPv6 网和正常网络下都进行了测试可以正常使用的;
而对于 Reachability 中 reachabilityForInternetConnection 方法在非 iOS9 下有bug的问题,我就参考了AF的处理方法,进行系统版本判断,经过测试,可以正常使用;
相关链接:
preflight check with SCNetworkReachabilityCreateWithAddress using a zero IPv6 address
[转自GeekerProbe]-Support for IPv6