Question
I'm developing a native web view application that works well, but I'm facing an issue with cookie persistence. Every time I close and restart the app, all login/cookie information is lost.
Interestingly, HTML5 Web Storage (local db) gets restored properly, but cookies aren't preserved. Here's the basic code structure I'm using (from a Samsung example):
[Code example remains the same as in original question]
Answer
Problem Understanding
The issue occurs because cookies in the native web view aren't being persisted between app sessions, while Web Storage data is preserved. This is a common challenge when working with web views in Tizen applications.
Solution Methods
-
Using EWebKit Cookie Management APIs:
- Utilize the EWebKit Cookie Manager functions to properly handle cookie persistence
- Key functions to use:
ewk_cookie_manager_persistent_storage_set()Ewk_Cookie_Persistent_StorageEwk_Cookie_Accept_Policy
-
JavaScript Workaround:
- Save cookies to LocalStorage via JavaScript when the app is running
- Read from LocalStorage and recreate cookies when the app starts
- This approach provides a reliable alternative when native cookie persistence fails
Code Examples
For the native API solution, you would need to implement something like:
// Get the cookie manager
Ewk_Cookie_Manager* cookie_manager = ewk_context_cookie_manager_get(ewk_view_context_get(web_view));
// Set persistent storage
ewk_cookie_manager_persistent_storage_set(cookie_manager, "/path/to/cookie/file", EWK_COOKIE_PERSISTENT_STORAGE_TEXT);
For the JavaScript workaround:
// Save cookies to localStorage
document.cookie.split(';').forEach(cookie => {
const [name, value] = cookie.split('=');
localStorage.setItem(`cookie_${name.trim()}`, value);
});
// Restore cookies from localStorage
Object.keys(localStorage).forEach(key => {
if (key.startsWith('cookie_')) {
const name = key.substring(7);
document.cookie = `${name}=${localStorage.getItem(key)}`;
}
});
Additional Tips
- Ensure your app has proper storage permissions in the manifest
- Test cookie persistence thoroughly as behavior might vary across Tizen versions
- Consider security implications when storing sensitive data in cookies