Mobile Web

Account: Managing Accounts and Account Information

This tutorial demonstrates how you can manage accounts and retrieve account information.

This feature is supported in mobile applications only.

Warm-up

Become familiar with the Account API basics by learning about:

Retrieving Accounts

Learning how to retrieve account information enables you to include account support into your applications:

  • To retrieve information about all available accounts, use the getAccounts() method of the AccountManager interface:
    function getAccountsSuccess(accounts)
    {
       for (var i = 0; i < accounts.length; i++)
       {
          /* Use the retrieved accounts */
       }
    }
    function getAccountsError(error)
    {
       console.log('Error: ' + error.message);
    }
    tizen.account.getAccounts(getAccountsSuccess, getAccountsError);
  • If you already know the ID of the account, you can get the Account object using the getAccount() method of the AccountManager interface:
    var account = tizen.account.getAccount(my_account_id);

Retrieving Providers

To create accounts, you must learn how to get access to account providers:

Note
To perform these operations, your application needs the http://tizen.org/privilege/account.read privilege.
  • Get a specific account provider with the given application ID using the getProviders() method of the ApplicationManager interface.

    If the current application is an account provider application (meaning that it contains the <tizen:account> element in its config.xml file), you can use the current application ID. Otherwise, get the ID of the current application using the getCurrentApplication() method of the ApplicationManager interface:

    var appId = tizen.application.getCurrentApplication().appInfo.id;
    var provider = tizen.account.getProvider(appId);
  • To get information about all available account providers, use the getProviders() method of the AccountManager interface:
    function getProvidersSuccess(providers)
    {
       /* Providers is an array whose members are providers with contact capability */
    }
    function getProvidersError(error)
    {
       console.log('Error: ' + error.message);
    }
    
    tizen.account.getProviders(getProvidersSuccess, getProvidersError, "http://tizen.org/account/capability/contact");

Managing Accounts

Creating, adding, updating, and deleting accounts is a basic account management skill:

Note
To perform these operations, your application needs the http://tizen.org/privilege/account.write privilege and must be the account provider.
  1. To create an account, first get an account provider. If your application is an account provider application (meaning that it contains the <tizen:account> element in its config.xml file), use the getProvider() method:
    var appId = tizen.application.getCurrentApplication().appInfo.id;
    var accountProvider = tizen.account.getProvider(appId);
  2. Create an instance of the Account interface:
    var account = new tizen.Account(accountProvider, {userName: 'admin', iconUri: 'path/to/icon.jpg'});
  3. Add the account to the account database:
    tizen.account.add(account);
  4. To update the account information, change the attributes of the Account object for the relevant account:
    account.userName = 'new username';
  5. To save the changed values, use the update() method of the AccountManager interface:
    tizen.account.update(account);
  6. To remove the account from the system, use the remove() method of the AccountManager interface, providing the account ID:
    tizen.account.remove(account.id);

Receiving Notifications on Account Changes

Learning how to register change listeners enables you to synchronize the view of your application with the changes in the account database:

Note
To perform these operations, your application needs the http://tizen.org/privilege/account.read privilege.
  1. Define a listener implementing the AccountChangeCallback interface:
    var accountChangeListener =
    {
       onadded: function(account)
       {
          /* Called when an account is added */
       },
       onremoved: function(accountId)
       {
          /* Called when an account is removed */
       },
       onupdated: function(account)
       {
          /* Called when a registered account is changed */
       }
    };
  2. Register the account listener using the addAccountListener() method of the AccountManager interface to start receiving notifications about changes:
    var watchId = tizen.account.addAccountListener(accountChangeListener);
  3. When notifications are no longer required, unregister the listener using the removeAccountListener() method of the AccountManager interface:
    tizen.account.removeAccountListener(watchId);

Managing Extended Account Data

Learning how to manage extended data for an account enables you to include account support into your applications:

  1. Retrieve the account object.
  2. Manage the extended data for the retrieved account:
    • To set extended data:

      Set the additional information with the setExtendedData() method:

      var key = 'nickname';
      var value = 'nickname of anonymous user';
      account.setExtendedData(key, value);
      Note
      To perform this operation, your application needs the http://tizen.org/privilege/account.write privilege.

      To overwrite the previous data value, set a new value with the same key:

      account.setExtendedData(key, 'nickname updated');
    • To get extended data:
      • To retrieve extended data value for a specific key, use the getExtendedData() method:
        var key = 'accessToken';
        var value = account.getExtendedData(key); 
        Note
        To perform this operation, your application needs the http://tizen.org/privilege/account.read privilege.
      • To retrieve all extended data for an account, use the asynchronous version of the getExtendedData() method. The success callback contains an array of the extended data key-value pairs.
        account.getExtendedData(function(extendedData)
                                {
                                   for (var i = 0; i < extendedData.length; i++)
                                   {
                                      var key = extendedData.key;
                                      var value = extendedData.value;
                                      console.log(key + " : " + value);
                                   }
                                },
                                function(e)
                                {
                                   console.log("Error : " + e.message);
                                });
Go to top