iOS Question WebView Post and cookies

iCAB

Well-Known Member
Licensed User
Longtime User
Hi All,

Can someone help provide the equivalent B4I code to the one below
B4X:
Private Sub InvokeLoginURL(UrlToInvoke As String, UserName As String, Password As String )

    If WebView1.IsInitialized = False Then
        WebView1.Initialize("WebView1")
        Activity.AddView(WebView1, 0, 0%y, 100%x, 100%y)
    End If

    Dim NativeMe As JavaObject
    NativeMe.InitializeContext
    NativeMe.RunMethod("SetAcceptThirdPartyCookies", Array As Object(WebView1, True))
    
    CookieManager1.RemoveAllCookies
    CookieManager1.SetAcceptCookies(True)
    
    

    Private tempPostUrl As String = UrlToInvoke
    Private tempPostString As String = "User=XXX&Password=YYY".Replace("XXX",UserName).Replace("YYY",Password)
    
    Private bytesOfPOstRequest() As Byte
    bytesOfPOstRequest=tempPostString.GetBytes("UTF8")
    
    Private jo As JavaObject = WebView1
    jo.RunMethod("postUrl",Array As Object(tempPostUrl,bytesOfPOstRequest))
End Sub



Sub WebView1_PageFinished (Url As String)
    
Try   
    If CookieManager1.HasCookies Then
        Log(CookieManager1.GetCookie(Url))
    Else
        LogColor("No cookies found", Colors.White)
    End If
Catch
    LogColor(LastException, Colors.Red)
End Try       

End Sub

#If JAVA
import android.webkit.CookieManager;
import android.webkit.WebView;

public void SetAcceptThirdPartyCookies(WebView webview, boolean accept){
    CookieManager.getInstance().setAcceptThirdPartyCookies(webview, accept);
}
#End If
 
Last edited:

iCAB

Well-Known Member
Licensed User
Longtime User
Why not make this request with iHttpUtils2?
I have no control over the server-side. The request is supposed to display a login page, where the user enters his password and invokes the login process by clicking a button on the page (in theory the password in the example above is never used).
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
You don't need to.

If you know the target url then just send a request with HttpJob. It will be simpler.
Can you please provide an example on how to display the page in a web view using HttpJob?
It is a requirement to present the webpage to the user.
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
There is no such thing.

Can you post the B4A code that calls InvokeLoginURL?

The code is in the first post, here is a copy

B4X:
    Private tempPostUrl As String = UrlToInvoke
    Private tempPostString As String = "User=XXX&Password=YYY".Replace("XXX",UserName).Replace("YYY",Password)
    
    Private bytesOfPOstRequest() As Byte
    bytesOfPOstRequest=tempPostString.GetBytes("UTF8")
    
    Private jo As JavaObject = WebView1
    jo.RunMethod("postUrl",Array As Object(tempPostUrl,bytesOfPOstRequest))


Thanks a lot
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
No. Where do Username and password come from?

The post method is expecting 2 parameters user name" and "password" but they don't need to have any data
Therefore the contents of "user name" and "password" can be empty.
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Erel, do you want me to PM you a sample project with a working URL and everything.
I can't post the URL in here
 
Upvote 0
Top