defold (official)
defold/extension-permissions/permissions
Functions and constants for interacting with permissions related APIs
- GitHub: defold/extension-permissions — pinned to
1.1.1(released while Defold 1.13.1 was current)- Pick a release from defold/extension-permissions releases and add its Source code (zip) URL (or a packaged
.zipasset, if the library ships one) togame.projectunder[project]dependencies, then Fetch Libraries in the Defold editor. - Run
bunx @defold-typescript/cli resolveto materialize its types. - Import it under a namespace alias of your choice:
import * as permissions from "permissions"
- Pick a release from defold/extension-permissions releases and add its Source code (zip) URL (or a packaged
Functions
permissions.check(permission: string): number
Determine whether you have been granted a particular permission.
local result = permissions.check("android.permission.ACCESS_NETWORK_STATE")
if result == permissions.PERMISSION_DENIED then
-- You can directly ask for the permission.
elseif result == permissions.PERMISSION_GRANTED then
-- You can use the API that requires the permission.
elseif result == permissions.PERMISSION_SHOW_RATIONALE then
-- In an educational UI, explain to the user why your app requires this
-- permission for a specific feature to behave as expected, and what
-- features are disabled if it's declined. In this UI, include a
-- "cancel" or "no thanks" button that lets the user continue
-- using your app without granting the permission.
endParameters
permission:string— A constant that represent permission, for exampleandroid.permission.ACCESS_NETWORK_STATE
Returns
result:number— A result can be one of the predefined constants belowpermissions.PERMISSION_GRANTEDpermissions.PERMISSION_DENIEDpermissions.PERMISSION_SHOW_RATIONALE
permissions.request(request_tbl: table, callback: function)
Requests permissions to be granted to this application.
local permissions_table = {"android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.READ_CONTACTS"}
permissions.request(permissions_table,
function(self, result)
for permission, result in pairs(result) do
if result == permissions.PERMISSION_DENIED then
-- You can directly ask for the permission.
elseif result == permissions.PERMISSION_GRANTED then
-- You can use the API that requires the permission.
elseif result == permissions.PERMISSION_SHOW_RATIONALE then
-- In an educational UI, explain to the user why your app requires this
-- permission for a specific feature to behave as expected, and what
-- features are disabled if it's declined. In this UI, include a
-- "cancel" or "no thanks" button that lets the user continue
-- using your app without granting the permission.
end
end
end)Parameters
request_tbl:table— An array with constants that represent permissions.callback:function— A callback taking the following parametersself:object— The calling scriptresult:table— A table where key is permission string and key is one of the following constantspermissions.PERMISSION_GRANTEDpermissions.PERMISSION_DENIED
Constants
permissions.PERMISSION_GRANTED
The permission has been granted to the given package.
permissions.PERMISSION_DENIED
The permission has not been granted to the given package.
permissions.PERMISSION_SHOW_RATIONALE
Explain why your app needs the permission Android doc