A basic plugin for setting, getting, and deleting cookies inside your Corona HTML5 apps.
-
Download the Corona HTML5 Cookie Plugin repository.
-
Move plugin/cookie.lua and plugin/cookie_js.js to the root of your HTML5 project.
-
Require the plugin in your code where you need it.
local cookie = require("cookie")
Set the cookie data.
cookie.set(name, value[, expiryDays])
Arguments
Name | Description | Type | Required |
---|---|---|---|
name | The name of the cookie. | String | Y |
value | The value of the cookie. | String | Y |
expiryDays | Days till expiration. Otherwise, lasts until deleted. | Number | N |
Returns
The String 'set' on success.
Example
local result = cookie.set("username", "develephant", 1) -- expire in 1 day
print(result) --> set
Get the cookie data.
cookie.get(name)
Arguments
Name | Description | Type | Required |
---|---|---|---|
name | The name of the cookie. | String | Y |
Returns
The cookie value as a String. If no cookie data exists, returns empty String.
Example
local username = cookie.get("username")
print(username) --> develephant
Delete the cookie data.
cookie.delete(name)
Arguments
Name | Description | Type | Required |
---|---|---|---|
name | The name of the cookie. | String | Y |
Returns
The String 'deleted' on success.
Example
local result = cookie.delete("username")
print(result) --> deleted
In the demo directory is the Corona project that you can compile yourself for testing. The demo does have some debug output that prints to the browser console. The plugin in the plugin directory does not output any debug info.
In the html5 directory is a compiled version of the demo project that you can push up to your server for testing.
-
Only tested on Google Chrome at this time.
-
In the demos, the loading bar is not shown because of the black background. Hopefully this will be a build setting option in the future.
-
Reload the browser after setting a cookie to see that it has saved the cookie.
-
You can also view the cookie data using the Google Chrome Developer Tools window. Click the Application tab, and then Cookies (on the left side menu).
©2018 C. Byerley (develephant)