All Projects → gotev → Android Cookie Store

gotev / Android Cookie Store

Licence: apache-2.0
Android InMemory and persistent Cookie Store for HttpURLConnection and OkHttp, with extensions to easily sync cookies in Android WebViews.

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Android Cookie Store

Net
Android上强大的网络请求
Stars: ✭ 344 (+138.89%)
Mutual labels:  okhttp, cookie
Store
A beautifully-simple framework-agnostic modern state management library.
Stars: ✭ 204 (+41.67%)
Mutual labels:  library, store
Agentweb
AgentWeb is a powerful library based on Android WebView.
Stars: ✭ 8,375 (+5715.97%)
Mutual labels:  cookie, webview
svelte-persistent-store
A Svelte store that keep its value through pages and reloads
Stars: ✭ 111 (-22.92%)
Mutual labels:  cookie, store
Tina
a powerful android network library base on okhttp
Stars: ✭ 32 (-77.78%)
Mutual labels:  library, okhttp
Easygo
基于Kotlin、OkHttp的声明式网络框架,像写HTML界面一样写网络调用代码
Stars: ✭ 40 (-72.22%)
Mutual labels:  library, okhttp
Chucker
🔎 An HTTP inspector for Android & OkHTTP (like Charles but on device)
Stars: ✭ 2,169 (+1406.25%)
Mutual labels:  library, okhttp
Circle Menu Android
⭕️ CircleMenu is a simple, elegant UI menu with a circular layout and material design animations. Android UI library made by @Ramotion
Stars: ✭ 1,807 (+1154.86%)
Mutual labels:  library
Rttr
C++ Reflection Library
Stars: ✭ 2,031 (+1310.42%)
Mutual labels:  library
Lc kicad lib
kicad production symbol and footprint library auto convert from JLC's integrate Altium Designer library
Stars: ✭ 140 (-2.78%)
Mutual labels:  library
Pipedrive
Complete Pipedrive API client for PHP
Stars: ✭ 138 (-4.17%)
Mutual labels:  library
Baffle
A tiny javascript library for obfuscating and revealing text in DOM elements. 😲
Stars: ✭ 1,721 (+1095.14%)
Mutual labels:  library
Tosin
Initialize a npm package with everything included, from CI to documentation website
Stars: ✭ 142 (-1.39%)
Mutual labels:  library
Autocser
AutoCSer is a high-performance RPC framework. AutoCSer 是一个以高效率为目标向导的整体开发框架。主要包括 TCP 接口服务框架、TCP 函数服务框架、远程表达式链组件、前后端一体 WEB 视图框架、ORM 内存索引缓存框架、日志流内存数据库缓存组件、消息队列组件、二进制 / JSON / XML 数据序列化 等一系列无缝集成的高性能组件。
Stars: ✭ 140 (-2.78%)
Mutual labels:  webview
Lib
🤖 Lets you focus on the stuff that matters
Stars: ✭ 142 (-1.39%)
Mutual labels:  library
Criterion
Microbenchmarking for Modern C++
Stars: ✭ 140 (-2.78%)
Mutual labels:  library
Coinbasepro Csharp
The unofficial .NET/C# client library for the Coinbase Pro/GDAX API
Stars: ✭ 143 (-0.69%)
Mutual labels:  library
Cordova Browser
Apache Cordova
Stars: ✭ 142 (-1.39%)
Mutual labels:  library
Android Base Mvp
Android Base MVP Concept with RXJava, Dagger, Event Bus, Retrofit, Glide, OkHTTP
Stars: ✭ 141 (-2.08%)
Mutual labels:  okhttp
Aspnetcore Angular Ngrx
🚀 An ASP.NET Core WebAPI Demo with an Angular Client using Ngrx store and effects and Signalr
Stars: ✭ 141 (-2.08%)
Mutual labels:  store

Android Cookie Store

Android Arsenal Android Weekly ktlint Download Maven Central PRs Welcome

Android InMemory and persistent Cookie Store for HttpURLConnection and OkHttp, with extensions to easily sync cookies in Android WebViews.

Why?

Neither HttpURLConnection nor OkHttp provides a native and rapid way of storing cookies persistently on Android. This library aims to fill this gap, by implementing the standard java.net.InMemoryCookieStore in Kotlin, with extendability in mind.

With this library you have:

  • super tiny footprint (the library is only a bunch of classes)
  • an in memory only cookie store
  • a shared preferences backed cookie store which can survive app reboots
  • possibility to extend both to provide your own custom implementation which best fits your needs without reinventing the wheel for cookie management

Compatibility

Android API 16+

Getting started

Add this to your dependencies:

implementation "net.gotev:cookie-store:x.y.z"

Replace x.y.z with Download

Usage

Create your Cookie Manager:

// Example extension function to demonstrate how to create both cookie stores
fun Context.createCookieStore(name: String, persistent: Boolean) = if (persistent) {
    SharedPreferencesCookieStore(applicationContext, name)
} else {
    InMemoryCookieStore(name)
}

val cookieManager = CookieManager(
    createCookieStore(name = "myCookies", persistent = true),
    CookiePolicy.ACCEPT_ALL
)

HttpURLConnection

To setup the default Cookie Manager:

CookieManager.setDefault(cookieManager)

OkHttp

Add the following dependency (suitable for JVM and Android):

implementation "net.gotev:cookie-store-okhttp:$cookieStoreVersion"

And when you build your OkHttpClient, set the Cookie Jar:

val okHttpClient = OkHttpClient.Builder()
    .cookieJar(JavaNetCookieJar(cookieManager))
    .build()

WebView

It's a common thing to obtain a cookie from an API and to open an authenticated web page inside an app which needs the cookie. You can find a complete working example in the demo app.

You have two ways of doing this:

  • Using WebKitSyncCookieManager
  • Using standard java.net.CookieManager

Using WebKitSyncCookieManager

val cookieManager = WebKitSyncCookieManager(
    createCookieStore(name = "myCookies", persistent = true),
    CookiePolicy.ACCEPT_ALL
)

Then follow standard instructions from the Usage section to setup HttpURLConnection or OkHttp according to your needs.

Incoming Cookies will be automatically synced to WebKit's CookieManager. Syncing is unidirectional from WebKitSyncCookieManager to android.webkit.CookieManager to have a single source of truth and to prevent attacks coming from URLs loaded in WebViews. If you need bi-directional sync, think twice before doing it.

To clear cookies:

cookieManager.removeAll()

This will clear both the CookieStore and WebKit's Cookie Manager.

Using standard java.net.CookieManager

Cookies syncing is entirely up to you and manual.

To copy all cookies from the cookie store to the WebKit Cookie Manager:

cookieManager.cookieStore.syncToWebKitCookieManager()

Remember to do this before loading any URL in your web view.

To remove all cookies from the Cookie Store:

cookieManager.cookieStore.removeAll()

To remove all cookies from WebKit Cookie Manager:

android.webkit.CookieManager.getInstance().removeAll()

That's all folks!

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].