iOS Question Webview: User agent and cookies

Martin Larsen

Active Member
Licensed User
Longtime User
Getting and setting the user agent

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:

Martin Larsen

Active Member
Licensed User
Longtime User
Since IOS 9 WKWebView has customUserAgent property,

Thanks! Your answer first let me google my way to this Stack Overflow post but when I tried with webview.customUserAgent i found that the webview didn't expose that property.

But now knowing what to look for, I then found this answer which was very simple:
B4X:
Dim Naobj As NativeObject = WKWebView1
Naobj.SetField("customUserAgent","YourUserAgent")

Apparently, NativeObject will cast the B4X object to the original ObjC object, thus exposing all the properties?

As for the cookies, I have found something very surprising, but nice in this case:

Cookies that are set from the server during the call via the initial HttpJob are automatically propagated to the webview. I log in via the HttpJob using the stored credentials and the server returns a session cookie. In B4A I must manually inject this session cookie into the webview. But in B4i, I have done nothing (because I didn't know how to!) and still it worked - the webview shows the users personal page. If I use some other credentials, I will see the corresponding data in the webview.

Although it is nice here, I find it a bit strange and a potential security issue. Something to be aware of, at least.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…