---
title: "（原创）iOS开发 兼容 IPv6-only网络 "
author: "3lll"
date: "2016-05-17T18:04:07Z"
tags: ["iOS开发"]
categories: ["移动开发"]
slug: "ios-ipv6-only"
draft: false
lastmod: "2016-05-23T20:02:33Z"
id: 2298819
url: "https://grayheart.iteye.com/blog/2298819"
---

苹果前段时间宣布从2016年6月1日起，App Store将实施全新策略，届时所有提交至苹果App Store的iOS应用申请必须要兼容面向硬件识别和网络路由的最新互联网协议–IPv6-only标准。因为可能会影响之后的上架，这两天花了些时间好好看了下相关的东西。大概整理如下：

**如何兼容IPV6**

1\. 使用更高层次的网络API（Use High-Level Networking Frameworks） 避免使用socket API

蓝色部分API 默认都支持IPv6

![](https://developer.apple.com/library/mac/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/art/NetworkingFrameworksAndAPIs_2x.png)

如果你的项目中使用的是类似WebKit这样的API或AFNetWorking、Alamofire等第三方库，那么恭喜你，相关的改动应该不会很多？为什么不是没有改动，请继续看。

2\. 不要使用IP地址

在有网络请求的地方，确保使用了域名，而不是192.168.0.1 这样的IP地址。

3.检查代码注意对IPv4和IPv6的支持情况

这些是IPV4特有的代码，如果有这些代码则需要修改

 

- `inet_addr()`
- `inet_aton()`
- `inet_lnaof()`
- `inet_makeaddr()`
- `inet_netof()`
- `inet_network()`
- `inet_ntoa()`
- `inet_ntoa_r()`
- `bindresvport()`
- `getipv4sourcefilter()`
- `setipv4sourcefilter()`

下面是IPv4和IPv6一些定义类型的对照，也要在代码中查找下。

IPv4

IPv6

`AF_INET`

`AF_INET6`

`PF_INET`

`PF_INET6`

`struct in_addr`

`struct in_addr6`

`struct sockaddr_in`

`struct sockaddr_in6`

`kDNSServiceProtocol_IPv4`

`kDNSServiceProtocol_IPv6`

    本身我们的项目中使用了AFNetWorking，网络接口的封装也是在此基础上，但是在第3项检查时，发现代码中只有 sockaddr\_in 而没有 sockaddr\_in6 ，为什么呢？因为我们的项目早先使用的是AFNetWorking的2.5.X版本，而官网上最新的版本是3.1.0。果断更新之，预料之中，最新的版本是支持IPV6的。所以提醒看到这里的同学，一定要检查2、3项，看是否确定支持IPV6，防止老版本的第三方库未支持IPV6。

参考资料：

 

Supporting IPv6-only Networks

[https://developer.apple.com/news/?id=05042016a](https://developer.apple.com/news/?id=05042016a)

Supporting IPv6 DNS64/NAT64 Networks

 

[https://developer.apple.com/library/mac/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/UnderstandingandPreparingfortheIPv6Transition/UnderstandingandPreparingfortheIPv6Transition.html#//apple\_ref/doc/uid/TP40010220-CH213-SW1](https://developer.apple.com/library/mac/documentation/NetworkingInternetWeb/Conceptual/NetworkingOverview/UnderstandingandPreparingfortheIPv6Transition/UnderstandingandPreparingfortheIPv6Transition.html#//apple_ref/doc/uid/TP40010220-CH213-SW1)

 

IPv6-only 的兼容性解决方案

[http://www.jianshu.com/p/8837739251ad?hmsr=toutiao.io&utm\_medium=toutiao.io&utm\_source=toutiao.io](http://www.jianshu.com/p/8837739251ad?hmsr=toutiao.io&utm_medium=toutiao.io&utm_source=toutiao.io)

 

Application Aspects of IPv6 Transition

[https://tools.ietf.org/html/rfc4038](https://tools.ietf.org/html/rfc4038)