简体   繁体   中英

How do I allow WebView to consistently control login state without losing login credentials?

I am creating my first android app and I thought I had finished it, but recently found out users were being logged out seemingly at random.

I'm using WebView to load a URL and the only time I manipulate the WebView is shouldOverrideUrlLoading within my WebViewClient in order to check for external links. Here's the gist of that:

@Override
public boolean shouldOverrideUrlLoading(WebView webview, WebResourceRequest request) {
    String RequestUrl = request.getUrl().toString();
    if(request.getUrl().getHost() != null) {
         changePage(RequestUrl);
    }
    return true;
}

public void changePage(String UrlString){
    Uri RequestUri = Uri.parse(UrlString);

    if(RequestUri.getHost() != null) {
        if ((UrlString.contains("http://") || UrlString.contains("https://")) && RequestUri.getHost().equals(getString(R.string.app_domain))) {
            webview.loadUrl(UrlString);
        } else {
            Intent intent = new Intent(Intent.ACTION_VIEW, RequestUri);
            webview.getContext().startActivity(intent);
        }
    }
}

This shouldn't be causing the problem.

I also use the website's login page to allow the user access with the purpose being to allow the web page to manage the login/logout state.

My problem is not that a user can't log in, or that they can't navigate the website through the app, but instead, after a while of navigating the WebView /website will lose the authentication provided by the initial login, effectively logging out the user. (All of this navigation I'm referring to is in-app/internal website links external works fine--I think).

Could this be due to the way I'm handling the links? Do I need to create some functionality to re-insert cookies on the request in order to create a consistent logged-in experience?

Please let me know if you need more information. Thanks!

Try this

 WebView.getSettings().setAppCacheEnabled(true);
   WebView.getSettings().setAppCachePath("/data/data" + getPackageName() + "/cache");
   WebView.getSettings().setSaveFormData(true);
   WebView.getSettings().setDatabaseEnabled(true);
   WebView.getSettings().setDomStorageEnabled(true);
    CookieManager.getInstance().acceptCookie();
    CookieManager.setAcceptFileSchemeCookies(true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM