Delphi XE10支持IOS IPV6的处理

苹果要求6月份新上架的APP统一支持IPV6,真是蛋疼,

我6月10号提交的APP,审核的时候给我消息,说在IPV6环境下不能登录,连不上服务器,

我在想,我在软件里面,也就用了一个IdHttp来Get一些接口的URL,以及图片,

而且用的可都是域名,没有使用IP4地址,比如(10.*.*.*),这样我知道是不行的,

既然我用的是域名,那为什么会不能连接到服务器,难道是Indy不支持IPV6,

然后上百度找了一下,确认Indy是支持IPV6的,

接着,根据苹果的教程搭建了IPV6的网络环境来测试,

然后发现,原来在IPV4下我Get一个URL,是可以直接IdHttp.Get('http://www.orangeui.cn'),

但是在IPV6的环境下,我需要在域名外面包一个方括号:IdHttp.Get('http://[www.orangeui.cn]'),

瞬间崩溃,

如果只是使用Http.Get来获取网络资源,可以把IdHttp控件换成原生的Http控件TNetHttpClient,这样不会有任何问题,

但是如果你用的是DataSnap或是DIOCP,就应该需要换另一种方式了,这种加[]的模式行不通。


解决方案:

1.服务器必须使用域名.不能使用IP地址
2.Indy的话,域名加[]
3.DataSnap的话,Params.Values['CommunicationIPVersion'] :='IP_IPv6';
4.当前网络环境的判断,如下代码:




{$IFDEF IOS}
uses
  StrUtils,
  Posix.NetDB,
  IdStackConsts;
{$ENDIF}






function IsSupportIPV6Host_IOS(const Host:String):Boolean;
{$IFDEF IOS}
var
  APHostEnt:PHostEnt;
{$ENDIF}
begin
  Result:=False;
{$IFDEF IOS}
  //苹果只要求IOS9以上的系统运行APP支持IPV6
  //IOS版本要大于9
  if TOSVersion.Check(9) then
  begin
    try
      FMX.Types.Log.d('OrangeUI IsSupportIPV6Host_IOS '+Host);
      //如果当前环境是IPV4,APHostEnt为nil
      APHostEnt:=gethostbyname2(MarshaledAString(TEncoding.UTF8.GetBytes(Host)),Id_PF_INET6);
      Result:=(APHostEnt<>nil);
      FMX.Types.Log.d('OrangeUI IsSupportIPV6Host_IOS IsIPV6 '+BoolToStr(Result));
    except
      //但是在IPV4切换到IPV6,程序没有退出的时候,会报错
    end;
  end;
{$ENDIF}
end;








//Indy
  if IsSupportIPV6Host_IOS('www.orangeui.cn') then
  begin
    IdHttp.Get('http://[www.orangeui.cn]');
  end
  else
  begin
    IdHttp.Get('http://www.orangeui.cn');
  end;






//DataSnap


var
  Index:Integer;
  IPV4Param:String;
  IPV6Param:String;
begin
  //根据网络环境判断是IPV4还是IPV6
  //CommunicationIPVersion=IP_IPv6
  IPV4Param:=TDBXPropertyNames.CommunicationIPVersion+'=IP_IPv4';
  IPV6Param:=TDBXPropertyNames.CommunicationIPVersion+'=IP_IPv6';
  Index:=ClientModuleUnit2.ClientModule2.SQLConnection1.Params.IndexOf(IPV4Param);
  if Index<>-1 then
  begin
    ClientModuleUnit2.ClientModule2.SQLConnection1.Params.Delete(Index);
  end;
  Index:=ClientModuleUnit2.ClientModule2.SQLConnection1.Params.IndexOf(IPV6Param);
  if Index<>-1 then
  begin
    ClientModuleUnit2.ClientModule2.SQLConnection1.Params.Delete(Index);
  end;


  //www.orangeui.cn换成自己的域名
  if IsSupportIPV6Host_IOS('www.orangeui.cn') then
  begin
    ClientModuleUnit2.ClientModule2.SQLConnection1.Params.Add(IPV6Param)
  end
  else
  begin
    ClientModuleUnit2.ClientModule2.SQLConnection1.Params.Add(IPV4Param)
  end;


  ClientModuleUnit2.ClientModule2.SQLConnection1.Connected:=True;
  Self.Edit2.Text:=ClientModuleUnit2.ClientModule2.ServerMethods1Client.ReverseString(Self.Edit1.Text);
end;







你可能感兴趣的:(Delphi移动开发)