Getting and setting the user agent
In B4A I can set get and the user agent using using Reflector:
And in this post is a snippet that should set the user agent in B4i:
However, it doesn't work. No errors thrown, it just doesn't do anything. The user agent is the original from my iPad.
Am I missing a step?
Similarly, how do I get the user agent from the webview? I know I can get it from injected javascript, but can I do in a similar fashion like above?
Setting cookies
Here is an example of getting the webview's cookies:
In B4A I can set the cookies using CookieManager. How can I set the cookies it in B4i?
The reason I need to do this is that the login to the app is done with a HttpJob with the credentials. Then the cookies are retrieved like this:
In B4A I can set get and the user agent using using Reflector:
B4X:
Dim r As Reflector
r.Target = webview
r.Target = r.RunMethod("getSettings")
' Get user agent
userAgent = r.RunMethod("getUserAgentString")
' Set user agent
r.RunMethod2("setUserAgentString", userAgent, "java.lang.String")
And in this post is a snippet that should set the user agent in B4i:
B4X:
...
SetUserAgent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36")
webview.LoadUrl("https://www.whatsmyua.info/")
---
Sub SetUserAgent(UserAgent As String)
Dim NaObj As NativeObject = Me
NaObj.RunMethod("SetUserAgent:",Array(UserAgent))
#If Objc
-(void)SetUserAgent:(NSString*)UserAgent{
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:UserAgent, @"UserAgent", nil];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
}
#End if
End Sub
However, it doesn't work. No errors thrown, it just doesn't do anything. The user agent is the original from my iPad.
Am I missing a step?
Similarly, how do I get the user agent from the webview? I know I can get it from injected javascript, but can I do in a similar fashion like above?
Setting cookies
Here is an example of getting the webview's cookies:
B4X:
Sub WebView1_PageFinished (Success As Boolean, Url As String)
Dim no As NativeObject
Dim cookies As List = no.Initialize("NSHTTPCookieStorage").RunMethod("sharedHTTPCookieStorage", Null).RunMethod("cookies", Null)
For Each cookie As NativeObject In cookies
Log($"Name: ${cookie.GetField("name")}, value: ${cookie.GetField("value")}"$)
Next
End Sub
In B4A I can set the cookies using CookieManager. How can I set the cookies it in B4i?
The reason I need to do this is that the login to the app is done with a HttpJob with the credentials. Then the cookies are retrieved like this:
B4X:
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
cookies = j.Response.GetHeaders.Get("set-cookie")
End If
...
CookieManager1.SetAcceptCookies(True)
CookieManager1.SetCookie("https://www.example.com", cookies)
...
Last edited: