背景
WWDC2015蘋果宣布在ios9支持純ipv6的網絡服務,并且要求2016年提交到AppStore的應用必須適配純ipv6的網絡環境,要求適配的系統版本ios9及以上
5.3.4p4以后的Unity已經對支持IPV6,如果你的項目里只用到WWW 或者 UnityWebRequest,恭喜,你不用做任何事情,只需要搭建ipv6的網絡環境,然后在ipv6的環境測試即可Unity and IPV6 Support
適配IPV6
如果項目中使用了非www,UnityWebRequest的其他網絡請求方式,就需要把ipv4的地址轉換成ipv6進行請求,所以在純ipv6的環境下,是可以使用ipv4的ip進行網絡訪問的,ios9會把ipv4 的地址轉換成ipv6的地址
git上已經ipv4轉ipv6的項目,直接拿來用,不用自己寫oc腳本了,哈哈 unity-ios-ipv6-ready
具體步驟如下
1、把git項目里的 IPv6.m和 IPv6.h 放到 Plugins/IOS下
2、模仿git項目中的DemoTest.cs 寫出以下接口
//調用ios插件轉換ipv4到ipv6
[DllImport ("__Internal")]
private static extern string getIPv6 (string host);
/// <summary>
/// 拿當前的ip地址或者域名來獲取對應ipv6的地址,,如果當前環境不支持ipv6,返回 當前的ipv4地址或者對應域名
/// </summary>
/// <param name="hostOrHostName">Host or host name.</param>
public static string GetIpV6 (string hostOrHostName)
{
string ip = hostOrHostName;
#if UNITY_IPHONE &&!UNITY_EDITOR
if (IsIPAdress (hostOrHostName)) {
try{
ip = getIPv6 (hostOrHostName);
if (!string.IsNullOrEmpty (ip)) {
string[] tmp = System.Text.RegularExpressions.Regex.Split (ip, "&&");
if (tmp != null && tmp.Length >= 2) {
string type = tmp[1];
if (type == "ipv6") {
ip = tmp[0];
Debug.Log ("---ipv6--- " + ip);
}else if(type == "ipv4") {
ip = tmp[0];
Debug.Log ("---ipv4--- " + ip);
}
}
}
}catch(Exception e){
Debug.LogErrorFormat ("GetIPv6 error: {0}", e.Message);
}
} else {
ip = GetIPV6Adress (hostOrHostName);
}
#else
return hostOrHostName;
#endif
Debug.Log ("hostOrHostName: -----" + hostOrHostName + " -------- ip " + ip);
return ip;
}
//判斷str是域名還是ip
private static bool IsIPAdress (string str)
{
Match match = Regex.Match (str, @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b");
return match.Success;
}
/// <summary>
/// 獲取域名對應ipv6地址
/// </summary>
/// <returns>The IP v6 adress.</returns>
/// <param name="hostName">Host name.</param>
private static string GetIPV6Adress (string hostName)
{
//基礎操作系統和網絡適配器是否支持 Internet 協議版本 6 (IPv6)。 ,,并且域名不為null
if (!System.Net.Sockets.Socket.OSSupportsIPv6 || string.IsNullOrEmpty (hostName))
return null;
System.Net.IPHostEntry host;
string connectIP = "";
try {
host = System.Net.Dns.GetHostEntry (hostName);
foreach (System.Net.IPAddress ip in host.AddressList) {
if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) {
connectIP = ip.ToString ();
} else if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6) {
connectIP = ip.ToString ();
}
}
} catch (Exception e) {
Debug.LogErrorFormat ("GetIPAddress error: {0}", e.Message);
}
Debug.Log ("---connectIP--- " + connectIP);
return connectIP;
}
//傳一個url轉換成ipv6 的地址
public string FinalUrl(string url)
{
#if UNITY_IPHONE &&!UNITY_EDITOR
string[] strs = url.Split ('/');
if (strs.Length < 2)
return url;
string hostOrName = strs [2];
string finalIp = "";
//如果有端口去掉端口
if (hostOrName.Contains (":")) {
hostOrName = hostOrName.Split (':') [0];
}
finalIp =GetIpV6 (hostOrName);
//解析后的域名,通過是否包含冒號來判斷是ipv6還是ipv4如果是ipv6格式的加上[] 不是ivp6格式不需要加,,,這塊比較坑 不加[] 會報錯,,非法的端口,,
if (finalIp.Contains (":")) {
finalIp = string.Format ("[{0}]", finalIp);
}
string finalUrl = url.Replace (hostOrName, finalIp);
return finalUrl;
#endif
//只有在蘋果真機上才會處理IP 其他情況直接返回 url
return url;
}
3、把項目里所有網絡請求的地方檢查一遍,把非WWW和UnityWebRequest的地方 的url 轉換一下
這樣的OK了,打包,在ipv6網絡環境下測試就行了
最后附上 ipv6 的搭建方法IPV6熱點搭建