Last post we explored the Contact API, but ran into permissions problems. This post, we’ll show the fix and show the last part of the contacts API. Try the (working) demo and check out the source code.
The most destructive API call in is by far the clear() command. It will remove all contacts from the device. The system does not step in and warn the user while the command is happening, so the duty is delegated to the application. In the future we will implement an alert dialog for this particular demo, but for now here is the code.
var clearRequest = navigator.mozContacts.clear();
clearRequest.onsuccess = function () {
console.log('All contacts have been removed.');
}
clearRequest.onerror = function () {
console.error('Contacts were not cleared');
}
According to the permissions list page, accessing the contacts API requires something like the following in the application manifest
"permissions": {
"contacts": {
"access": "readwrite",
"description": "Needed to read, create, and modify contacts"
}
},
Where the possible values of access are
readonlyreadwritereadcreatecreateonlyLooking at the permissions page again, you can notice that the Default Granted column has the value Prompt for the contacts API. This means that the user will be given a prompt before the app is given access to the API. The user can deny or allow the app the access to the API at this prompt.
However, when we test the app, no such prompt comes up and the app reports error on every contact API. The error name is Not Allowed. Intuitively, there is an error with the permissions.
The fix is a simple one, all it requires is that the app manifest contain the following
"type": "privileged"
Here’s the explanation. The contact API is a privileged API. An application can be privileged when it is installed through the marketplace. But for the system to recognize the application as privileged, we must specify it in the manifest as stated above.