如何在IOS Delphi上的TWebbrowser中设置useragent

时间:2018-06-11 09:05:44

标签: ios iphone delphi ipad ios-simulator

我尝试通过JS设置用户代理,但它不起作用,UA没有改变。有谁能给我工作解决方案?

// js:= 'function setUserAgent(t,e){if(t.navigator.userAgent!=e){var r={get:function(){return e}};try{Object.defineProperty(t.navigator,"userAgent",r)}catch(n){t.navigator=Object.create(navigator,{userAgent:r})}}}; setUserAgent(window, '''+UA+''');';
    js:='Object.defineProperty(navigator, ''userAgent'', {get: function () { return "'+UA+'"; }});';

    WebBrowser.EvaluateJavaScript(js);

1 个答案:

答案 0 :(得分:2)

uses
  iOSapi.Foundation, Macapi.Helpers;

function StandardUserDefaults: NSUserDefaults;
begin
  Result := TNSUserDefaults.Wrap(TNSUserDefaults.OCClass.StandardUserDefaults);
end;

function StrToObjectID(const AStr: string): Pointer;
begin
  Result := NSObjectToID(StrToNSStr(AStr));
end;

procedure SetUserAgent;
var
  LUserAgentDict: Pointer;
begin
  LUserAgentDict := TNSDictionary.OCClass.dictionaryWithObject(StrToObjectID('Mozilla/Fake version 123.4.beta'), StrToObjectID('UserAgent'));
  StandardUserDefaults.registerDefaults(TNSDictionary.Wrap(LUserAgentDict));
end;

更改用户代理的值以适合