Titanium.Contacts
> Titanium.Contacts

The top-level Contacts module, used for accessing and modifying the system contacts address book.

See examples for more information.

iOS Platform Notes

On iOS, the contacts database may be modified by an external application, causing any Person or Group objects you've retrieved to be out of sync with the database. The IDs of these objects are not guaranteed to remain the same, so updating an object when it is out of sync may have unpredictable results.

To avoid this, listen for the reload event. When you receive a reload event, you should assume that any existing Person or Group objects are invalid and reload them from the Contacts module before modifying them.

See the examples for sample use of the reload event.

If 'ABAddressBookErrorDomain error 0' occurs, it implies that you are not allowed to add or edit certain fields. Check your default account in the iOS settings under contacts. If it's not 'iCloud', mostly likely it will not support fields such as alternateBirthday or socialProfile.

Examples

Request access to the address book

var performAddressBookFunction = function(){...};
var addressBookDisallowed = function(){...};
if (Ti.Contacts.hasContactsPermissions()) {
    performAddressBookFunction();
} else {
    Ti.Contacts.requestContactsPermissions(function(e) {
        if (e.success) {
            performAddressBookFunction();
        } else {
            addressBookDisallowed();
        }
    });
}

Query Existing System Address Book Records

Output to the console all properties of all people.

var singleValue = [
  'recordId', 'firstName', 'middleName', 'lastName', 'fullName', 'prefix', 'suffix', 
  'nickname', 'firstPhonetic', 'middlePhonetic', 'lastPhonetic', 'organization', 
  'jobTitle', 'department', 'note', 'birthday', 'created', 'modified', 'kind'
];
var multiValue = [
  'email', 'address', 'phone', 'instantMessage', 'relatedNames', 'date', 'url'
];
var people = Ti.Contacts.getAllPeople();
Ti.API.info('Total contacts: ' + people.length);
for (var i=0, ilen=people.length; i<ilen; i++){
  Ti.API.info('---------------------');
  var person = people[i];
  for (var j=0, jlen=singleValue.length; j<jlen; j++){
    Ti.API.info(singleValue[j] + ': ' + person[singleValue[j]]);
  }
  for (var j=0, jlen=multiValue.length; j<jlen; j++){
    Ti.API.info(multiValue[j] + ': ' + JSON.stringify(person[multiValue[j]]));
  }
}

Add New System Address Book Records

Create two new records in the system address book. Note that the Titanium.Contacts.Person object is queried in the same way that it is created (as shown in previous example.)

Ti.API.info('Saving contact...');
Ti.Contacts.createPerson({
  firstName: 'Paul',
  lastName: 'Dowsett',
  address:{
    work:[
      {
        CountryCode: 'gb', // determines how the address is displayed
        Street: '200 Brook Drive\nGreen Park',
        City: 'Reading',
        County: 'Berkshire',
        Country: 'England',
        ZIP: 'RG2 6UB'
      },
      {
        CountryCode: 'gb', // determines how the address is displayed
        Street: '1 St Pauls Road\nClerkenwell',
        City: 'City of London',
        State: 'London',
        Country: 'England',
        ZIP: 'EC1 1AA'
      }
    ],
    home:[
      {
        CountryCode: 'gb', // determines how the address is displayed
        Street: '2 Boleyn Court',
        City: 'London',
        State: 'Greenwich',
        Country: 'England',
        ZIP: 'SE10'
      }
    ]
  },
  birthday: '2012-01-01T12:00:00.000+0000',
  instantMessage:{
    home:[
      {
        service: 'AIM',
        username: 'leisureAIM'
      },
      {
        service: 'MSN',
        username: 'no_paul_here@msn.com'
      }
    ],
    work:[
      {
        service: 'AIM',
        username: 'seriousAIM'
      }
    ]
  },
  organization: 'Appcelerator',
  phone:{
    mobile: ['07900 000001', '07900 000002'],
    work: ['+44 (0)118 925 6128', '+44 (0)118 000 0000']
  },
  url:{
    homepage: ['www.google.com'],
    work: ['www.appcelerator.com', 'www.example.com']
  }
});
Ti.API.info('Contact saved');

Ti.API.info('Saving contact...');
var workAddress1 = {
  'CountryCode': 'us',
  'Street':  '440 N. Bernardo Avenue',
  'City': 'Mountain View',
  'State': 'California',
  'Country': 'United States',
  'ZIP': '94043'
};

Ti.Contacts.createPerson({
  firstName:'Arthur',
  lastName:'Evans',
  address:{
    'work':[workAddress1]
  }
});
Ti.API.info('Contact saved');

Repopulate contact data if it was modified externally

Listen for the reload event to repopulate the contact data if it was modified externally, for example, in the iOS Contacts app.

var win = Ti.UI.createWindow();
var table = Ti.UI.createTableView();

// Repopulate contact data
function reloadContacts() {
    var contacts = Ti.Contacts.getAllPeople();
    var data = [];
    for (var i = 0; i < contacts.length; i++) {
        var title = contacts[i].fullName;
        if (!title || title.length === 0) {
            title = "(no name)";
        }
        var row = Ti.UI.createTableViewRow({
            title: title
        });
        data.push(row);
    }
    table.data = data;
}

Ti.Contacts.addEventListener('reload', function(e){
    alert('Reloading contacts. Your contacts were changed externally!');
    reloadContacts();
});

// initial call to populate contact data
reloadContacts();

win.add(table);
win.open();
  • 0.8
  • 0.8
  • 0.8
Defined By

Properties

Titanium.Contacts
AUTHORIZATION_AUTHORIZED : Numberreadonly

A contactsAuthorization value indicating that the application is authorized to use the address book.

A contactsAuthorization value indicating that the application is authorized to use the address book.

This value is always returned on Android devices, as well as on iOS versions earlier than 6.0.

  • 2.1.3
  • 2.1.3
  • 2.1.3
Titanium.Contacts
AUTHORIZATION_DENIED : Numberreadonly

A contactsAuthorization value indicating that the application is not authorized to use the address book.

A contactsAuthorization value indicating that the application is not authorized to use the address book.

  • 2.1.3
  • 2.1.3
  • 2.1.3
Titanium.Contacts
: Numberreadonly
A contactsAuthorization value indicating that the application is not authorized to use the address book and the user...

A contactsAuthorization value indicating that the application is not authorized to use the address book and the user cannot change this application's status.

  • 2.1.3
  • 2.1.3
  • 2.1.3
Titanium.Contacts
AUTHORIZATION_UNKNOWN : Numberreadonly

A contactsAuthorization value indicating that the authorization state is unknown.

A contactsAuthorization value indicating that the authorization state is unknown.

  • 2.1.3
  • 2.1.3
  • 2.1.3
Titanium.Contacts
CONTACTS_KIND_ORGANIZATION : Numberreadonly

Specifies that a contact is an organization.

Specifies that a contact is an organization.

Used with the Titanium.Contacts.Person.kind property.

One of the group of contact "kind" constants CONTACTS_KIND_ORGANIZATION, and CONTACTS_KIND_PERSON.

Titanium.Contacts
CONTACTS_KIND_PERSON : Numberreadonly

Specifies that a contact is a person.

Specifies that a contact is a person.

Used with the Titanium.Contacts.Person.kind property. One of the group of contact "kind" constants CONTACTS_KIND_ORGANIZATION, and CONTACTS_KIND_PERSON.

Titanium.Contacts
CONTACTS_SORT_FIRST_NAME : Numberreadonly

Specifies that group members will be sorted by first name.

Specifies that group members will be sorted by first name.

Used with the Titanium.Contacts.Group.sortedMembers method. One of the group of contact group "sort" constants CONTACTS_SORT_FIRST_NAME, and CONTACTS_SORT_LAST_NAME.

Titanium.Contacts
CONTACTS_SORT_LAST_NAME : Numberreadonly

Specifies that group members will be sorted by last name.

Specifies that group members will be sorted by last name.

Used with the Titanium.Contacts.Group.sortedMembers method. One of the group of contact group "sort" constants CONTACTS_SORT_FIRST_NAME, and CONTACTS_SORT_LAST_NAME.

apiName : Stringreadonly

The name of the API that this proxy corresponds to.

The name of the API that this proxy corresponds to.

The value of this property is the fully qualified name of the API. For example, Button returns Ti.UI.Button.

  • 3.2.0
  • 3.2.0
  • 3.2.0
Indicates if the proxy will bubble an event to its parent. ...

Indicates if the proxy will bubble an event to its parent.

Some proxies (most commonly views) have a relationship to other proxies, often established by the add() method. For example, for a button added to a window, a click event on the button would bubble up to the window. Other common parents are table sections to their rows, table views to their sections, and scrollable views to their views. Set this property to false to disable the bubbling to the proxy's parent.

Default: true

  • 3.0.0
  • 3.0.0
  • 3.0.0
Titanium.Contacts
contactsAuthorization : Numberreadonly

Returns an authorization constant indicating if the application has access to the address book.

Returns an authorization constant indicating if the application has access to the address book.

Always returns AUTHORIZATION_AUTHORIZED on iOS pre-6.0 and Android devices.

If contactsAuthorization is AUTHORIZATION_RESTRICTED, you should not attempt to re-authorize: this will lead to issues.

This API can be assigned the following constants:

  • 2.1.3
  • 2.1.3
  • 2.1.3

The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.

The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.

If this property is set to a Window or TabGroup, then the corresponding Activity lifecycle event callbacks will also be called on the proxy. Proxies that require the activity lifecycle will need this property set to the appropriate containing Window or TabGroup.

  • 3.6.0
Defined By

Methods

Adds the specified callback as an event listener for the named event. ...

Adds the specified callback as an event listener for the named event.

Parameters

  • name : String

    Name of the event.

  • callback : Callback<Object>

    Callback function to invoke when the event is fired.

Returns

  • void
Applies the properties to the proxy. ...

Applies the properties to the proxy.

Properties are supplied as a dictionary. Each key-value pair in the object is applied to the proxy such that myproxy[key] = value.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Parameters

  • props : Dictionary

    A dictionary of properties to apply.

Returns

  • void
Titanium.Contacts
( [parameters] ) : Titanium.Contacts.Group
Creates and returns an instance of Titanium.Contacts.Group. ...

Creates and returns an instance of Titanium.Contacts.Group.

This method must be followed by save to commit its changes.

  • 0.8
  • 0.8

Parameters

Returns

Titanium.Contacts
( [parameters] ) : Titanium.Contacts.Person
Creates and returns an instance of Titanium.Contacts.Person, and commits all pending changes to the underlying conta...

Creates and returns an instance of Titanium.Contacts.Person, and commits all pending changes to the underlying contacts database.

  • 2.1.0
  • 0.8
  • 0.8

Parameters

Returns

Fires a synthesized event to any registered listeners. ...

Fires a synthesized event to any registered listeners.

Parameters

  • name : String

    Name of the event.

  • event : Dictionary

    A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.

Returns

  • void
Titanium.Contacts
( ) : Titanium.Contacts.Group[]
Gets all groups. ...

Gets all groups.

  • 0.8
  • 0.8

Returns

Titanium.Contacts
( limit ) : Titanium.Contacts.Person[]
Gets all people, unless a limit is specified. ...

Gets all people, unless a limit is specified.

Parameters

  • limit : Number

    Maximum number of people. Android only.

Returns

Gets the value of the apiName property. ...

Gets the value of the apiName property.

  • 3.2.0
  • 3.2.0
  • 3.2.0

Returns

  • String
Gets the value of the bubbleParent property. ...

Gets the value of the bubbleParent property.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Returns

  • Boolean
Titanium.Contacts
( ) : Number
Gets the value of the contactsAuthorization property. ...

Gets the value of the contactsAuthorization property.

  • 2.1.3
  • 2.1.3
  • 2.1.3

Returns

  • Number
Titanium.Contacts
( id ) : Titanium.Contacts.Group
Gets the group with the specified identifier. ...

Gets the group with the specified identifier. Deprecated for >= iOS9. Use getGroupByIdentifier instead.

Requires: iOS 8.0 and earlier

  • 0.8
  • 0.8

Parameters

  • id : Number

    Group identifier.

Returns

Gets the group with the specified identifier. ...

Gets the group with the specified identifier.

Requires: iOS 9.0 and later

  • 5.0.0
  • 5.0.0

Parameters

  • id : String

    Group identifier.

Returns

Gets the value of the lifecycleContainer property. ...

Gets the value of the lifecycleContainer property.

  • 3.6.0

Returns

Titanium.Contacts
( name ) : Titanium.Contacts.Person[]
Gets people with a firstName, middleName or lastName field, or a combination of these fields, that match the specifi...

Gets people with a firstName, middleName or lastName field, or a combination of these fields, that match the specified name.

Parameters

  • name : String

    Name to match.

Returns

Titanium.Contacts
( id ) : Titanium.Contacts.Person
Gets the person with the specified identifier. ...

Gets the person with the specified identifier. Deprecated for >= iOS9. Use getPersonByIdentifier instead.

Requires: iOS 8.0 and earlier

Parameters

  • id : Number

    Contact identifier.

Returns

Gets the person with the specified identifier. ...

Gets the person with the specified identifier.

Requires: iOS 9.0 and later

  • 5.0.0
  • 5.0.0
  • 5.0.0

Parameters

  • id : Number

    Contact identifier.

Returns

Titanium.Contacts
( ) : Boolean
Returns true if the app has contacts access. ...

Returns true if the app has contacts access.

  • 5.1.0
  • 5.1.0
  • 5.1.0

Returns

  • Boolean
Removes the specified callback as an event listener for the named event. ...

Removes the specified callback as an event listener for the named event.

Multiple listeners can be registered for the same event, so the callback parameter is used to determine which listener to remove.

When adding a listener, you must save a reference to the callback function in order to remove the listener later:

var listener = function() { Ti.API.info("Event listener called."); }
window.addEventListener('click', listener);

To remove the listener, pass in a reference to the callback function:

window.removeEventListener('click', listener);

Parameters

  • name : String

    Name of the event.

  • callback : Callback<Object>

    Callback function to remove. Must be the same function passed to addEventListener.

Returns

  • void
Titanium.Contacts
( group )
Removes a group from the address book. ...

Removes a group from the address book.

This method must be followed by save to commit its changes.

  • 0.8
  • 0.8

Parameters

Returns

  • void
Titanium.Contacts
( person )
Removes a contact from the address book. ...

Removes a contact from the address book.

On iOS:

This method must be followed by a save action to commit the data to the underlying database, which can be done explicitly using save or implicitly using createPerson. Although the Titanium.Contacts.Person object will still exist once committed, it will no longer be valid. Continuing to use it will result in unpredictable behavior, including crashes.

On Android:

This method will remove the person from the Contacts book automatically.

  • 2.1.0
  • 0.8
  • 0.8

Parameters

Returns

  • void
Titanium.Contacts
( callback )deprecated
If authorization is unknown, will bring up a dialog requesting permission. ...

If authorization is unknown, will bring up a dialog requesting permission.

deprecated since 5.1.0

Use <Titanium.Contacts.requestContactsPermissions> instead.

Note that the callback may be synchronous or asynchronous. That is, it may be called during requestAuthorization or much later. See the "Request access to the address book" example on how to best use this method.

  • 2.1.3
  • 2.1.3
  • 2.1.3

Parameters

Returns

  • void
Titanium.Contacts
( callback )
Requests for contacts access. ...

Requests for contacts access.

On Android, the request view will show if the permission is not accepted by the user, and the user did not check the box "Never ask again" when denying the request. If the user checks the box "Never ask again," the user has to manually enable the permission in device settings.

This method requests Manifest.permission.READ_CONTACTS on Android. If you require other permissions, you can also use Titanium.Android.requestPermissions.

In iOS 6, Apple introduced the Info.plist key NSContactsUsageDescription that is used to display an own description while authorizing contacts permissions. In iOS 10, this key is mandatory and the application will crash if your app does not include the key. Check the Apple docs for more information.

  • 5.1.0
  • 5.1.0
  • 5.1.0

Parameters

Returns

  • void
Titanium.Contacts
( )
Reverts all changes made by the previous save to the address book. ...

Reverts all changes made by the previous save to the address book. Deprecated for >= iOS9.

Requires: iOS 8.0 and earlier

  • 0.8
  • 0.8

Returns

  • void
Titanium.Contacts
( contacts )
Commits all pending changes to the underlying contacts database. ...

Commits all pending changes to the underlying contacts database.

On Android:

Takes an array of Titanium.Contacts.Person objects and saves changes for the specified contacts only.

  • 3.0.0
  • 0.8
  • 0.8

Parameters

Returns

  • void
Sets the value of the bubbleParent property. ...

Sets the value of the bubbleParent property.

  • 3.0.0
  • 3.0.0
  • 3.0.0

Parameters

  • bubbleParent : Boolean

    New value for the property.

Returns

  • void
Sets the value of the lifecycleContainer property. ...

Sets the value of the lifecycleContainer property.

  • 3.6.0

Parameters

Returns

  • void
Titanium.Contacts
( params )
Displays a picker that allows a person to be selected. ...

Displays a picker that allows a person to be selected.

Parameters

  • params : showContactsParams

    Argument containing parameters for this method. Optional on Android.

Returns

  • void
Defined By

Events

Titanium.Contacts
Fired when the database backing the contacts module is modified externally. ...

Fired when the database backing the contacts module is modified externally.

If you have an existing reference to a Person or Group object, you should obtain a new reference from the Contacts module. Using the existing object may result in unpredictable behavior, such as updating the wrong contact.

  • 3.1.0
  • 3.1.0

Properties

  • source : Object

    Source object that fired the event.

    •  
    •  
    •  
  • type : String

    Name of the event fired.

    •  
    •  
    •  
  • bubbles : Boolean

    True if the event will try to bubble up if possible.

    •  
    •  
    •  
  • cancelBubble : Boolean

    Set to true to stop the event from bubbling.

    •  
    •  
    •