iOS Question How to clear WebView cookies?

Haris Hafeez

Active Member
Licensed User
Longtime User
Hello All,

I know a similar question was asked earlier but the problem there and, therefore, the solution was quite different.

So, my requirement is that I am using a third party system to log people in(using Microsoft ID and APIs). The first step in this process is to have the user sign-in to their MS account using MS sign-in page after which they consent to my app using their credentials. While this works for the first account that is added, the second time the user tries to link another account with my app, the webview presents the previously authorized credentials(I assume from cookies) to the MS servers which then return the success page.

I am using a new page that loads the webview containing layout whenever the user tries to link another account. So a new webview object is obviously being used but the cookies (I think) are sticking around as they probably should. Also, I have tried using the following code but it doesn't seem to do anything.

B4X:
    Dim no As NativeObject
no.Initialize("NSURLCache").RunMethod("sharedURLCache", Null).RunMethod("removeAllCachedResponses", Null)

How do I clear the webview so that it always thinks it is a new page request, kind of incognito mode in chrome?

Thanks.
 
Last edited:

Haris Hafeez

Active Member
Licensed User
Longtime User
Hello everyone.

I guess I forgot that we've got inline Objective-C support. A bit of Google resulted in the solution.

For everyone else, here's what you need to do:

In your module, add the following:

B4X:
#If OBJC

- (void) clearCookies {
    for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) {

        [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
    }
}

#End If

At the point where you want to clear the cookies(in my case it was on page disappear), do the following:
B4X:
Private Sub page_Disappear
    Dim no As NativeObject = Me
    no.RunMethod("clearCookies", Null)
End Sub

Happy coding :)
 
Upvote 0

Baris Karadeniz

Active Member
Licensed User
When I run the code above, I receive an error below. (Method not found; clearCookies) What can be the reason?

Application_Start
Application_Active
Location is updated
Location is updated
Location is updated
Location is updated
Error occurred on line: 276 (Main)
Method not found: clearCookies, target: <b4i_main: (null)>
Stack Trace: (
CoreFoundation <redacted> + 150
libobjc.A.dylib objc_exception_throw + 38
CoreFoundation <redacted> + 0
result +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 426
result -[B4INativeObject RunMethod::] + 164
result -[b4i_main _btndrvusermanual_click] + 878
CoreFoundation <redacted> + 68
CoreFoundation <redacted> + 292
result +[B4I runDynamicMethod:method:throwErrorIfMissing:args:] + 1786
result -[B4IShell runMethod:] + 574
result -[B4IShell raiseEventImpl:method:args::] + 1998
result -[B4IShellBI raiseEvent:event:params:] + 1442
result __33-[B4I raiseUIEvent:event:params:]_block_invoke + 74
libdispatch.dylib <redacted> + 10
libdispatch.dylib <redacted> + 22
libdispatch.dylib _dispatch_main_queue_callback_4CF + 1532
CoreFoundation <redacted> + 8
CoreFoundation <redacted> + 1590
CoreFoundation CFRunLoopRunSpecific + 516
CoreFoundation CFRunLoopRunInMode + 108
GraphicsServices GSEventRunModal + 160
UIKit UIApplicationMain + 144
result main + 108
libdyld.dylib <redacted> + 2
)
 
Upvote 0

Baris Karadeniz

Active Member
Licensed User
I have 3 webview. I just want to delete cookies in one of them. In this case, will I call "Private Sub page_Disappear" at that webview? Or when I call page_Disappear it deletes all cookies?
 
Upvote 0
Top