Address Book
Learn how to store and sync the phone contact list with QuickBlox.
Address Book API provides an interface to work with the phone address book. Upload it to the server and retrieve already registered QuickBlox users from your address book.
Visit our Key Concepts page to get an overall understanding of the most important QuickBlox concepts.
Before you begin
- Register a QuickBlox account. This is a matter of a few minutes and you will be able to use this account to build your apps.
- Configure QuickBlox SDK for your app. Check out our Setup page for more details.
- Create a user session to be able to use QuickBlox functionality. See our Authentication page to learn how to do it.
Upload address book
First of all, you need to upload your address book to the backend. It's a normal practice to do a full upload for the 1st time and then upload new contacts on future app logins.
let contact = QBAddressBookContact()
contact.name = "Apple service"
contact.phone = "1-800-275-2273"
let contactsArray = [contact]
let addressBook: NSOrderedSet = NSOrderedSet(array: contactsArray)
QBRequest.uploadAddressBook(withUdid: nil, addressBook: addressBook, force: false, successBlock: { (addressBookUpdates) in
// process response
}, errorBlock: { (response) in
// handle errors
debugPrint("[error: \(response.error?.error)")
})
QBAddressBookContact *contact = [QBAddressBookContact new];
contact.name = @"Apple service";
contact.phone = @"1-800-275-2273";
NSMutableOrderedSet<QBAddressBookContact *>* addressBook = [NSMutableOrderedSet orderedSet];
[addressBook addObject:contact];
[QBRequest uploadAddressBookWithUdid:nil addressBook:[addressBook copy] force:NO successBlock:^(QBAddressBookUpdates * _Nonnull updates) {
// process response
} errorBlock:^(QBResponse * _Nonnull response) {
// handle errors
NSLog(@"errors=%@", response.error.error);
}];
- You also can edit an existing contact by providing a new name for it.
- You also can upload more contacts, not just all in one request. They will be added to your address book on the backend. If you want to override the whole address book on the backend just provide the
force:true:
option. - A device
UDID
is used in cases where a user has 2 or more devices and contacts sync is off. Otherwise, a user has a single global address book.
Retrieve address book
You can retrieve your uploaded address book using the code snippet below.
QBRequest.addressBook(withUdid: nil, successBlock: { (addressBookContacts) in
// process response
}, errorBlock: { (response) in
// handle errors
debugPrint("[error: \(response.error?.error)")
})
[QBRequest addressBookWithUdid:nil successBlock:^(NSArray<QBAddressBookContact *> * _Nonnull contacts) {
// process response
} errorBlock:^(QBResponse * _Nonnull response) {
// handle errors
NSLog(@"errors=%@", response.error.error);
}];
Update contacts
If you need to update the name or phone number in your address book contacts, you should use the code sample below.
let contactToUpdate = QBAddressBookContact()
contactToUpdate.phone = "380955868675" // Existing phone number
contactToUpdate.name = "QuickBlox in Ukraine" // Name to replace in the Server
let contactsArray = [contactToUpdate]
let contactsToUpdate: NSOrderedSet = NSOrderedSet(array: contactsArray)
QBRequest.uploadAddressBook(withUdid: nil, addressBook: contactsToUpdate, force: false, successBlock: { (addressBookUpdates) in
// process response
}, errorBlock: { (response) in
// handle errors
debugPrint("[error: \(response.error?.error)")
})
QBAddressBookContact *contactToUpdate = [QBAddressBookContact new];
contactToUpdate.name = @"QuickBlox in Ukraine"; // Name to replace in the Server
contactToUpdate.phone = @"380955868675"; // Existing phone number
NSMutableOrderedSet<QBAddressBookContact *>* contactsToUpdate = [NSMutableOrderedSet orderedSet];
[contactsToUpdate addObject:contactToUpdate];
[QBRequest uploadAddressBookWithUdid:nil addressBook:[contactsToUpdate copy] force:NO successBlock:^(QBAddressBookUpdates * _Nonnull updates) {
// process response
} errorBlock:^(QBResponse * _Nonnull response) {
// handle errors
NSLog(@"errors=%@", response.error.error);
}];
Delete contacts
You can delete contacts by using the code snippet below.
let contactToDelete = QBAddressBookContact()
contactToDelete.phone = "380955868675" // Existing phone number
contactToDelete.destroy = true //To mark as Deleted
let contactsArray = [contactToDelete]
let contactsToDelete: NSOrderedSet = NSOrderedSet(array: contactsArray)
QBRequest.uploadAddressBook(withUdid: nil, addressBook: contactsToDelete, force: false, successBlock: { (addressBookUpdates) in
// process response
}, errorBlock: { (response) in
// handle errors
debugPrint("[error: \(response.error?.error)")
})
QBAddressBookContact *contactToDelete = [QBAddressBookContact new];
contactToDelete.phone = @"380955868675";
contactToDelete.destroy = YES; //To mark as Deleted
NSMutableOrderedSet<QBAddressBookContact *>* contactsToDelete = [NSMutableOrderedSet orderedSet];
[contactsToDelete addObject:contactToDelete];
[QBRequest uploadAddressBookWithUdid:nil addressBook:[contactsToDelete copy] force:NO successBlock:^(QBAddressBookUpdates * _Nonnull updates) {
// process response
} errorBlock:^(QBResponse * _Nonnull response) {
// handle errors
NSLog(@"errors=%@", response.error.error);
}];
Retrieve registered users
Using this request you can easily retrieve the QuickBlox users - your address book contacts that are already registered in your app. Users are matched with address book contacts by phone number, so user and address book contact must have the same phone number to be included in response.
QBRequest.registeredUsersFromAddressBook(withUdid: "your udid", isCompact: false, successBlock: { (users) in
// process response
}, errorBlock: { (response) in
// handle errors
debugPrint("[error: \(response.error?.error)")
})
[QBRequest registeredUsersFromAddressBookWithUdid:@"your udid" isCompact:NO successBlock:^(NSArray<QBUUser *> *users) {
// process response
} errorBlock:^(QBResponse * _Nonnull response) {
// handle errors
NSLog(@"errors=%@", response.error.error);
}];
If the isCompact
parameter is true
, the server will return only the userId
and phone
fields of the QBUser
model. Otherwise, all fields of the QBUser
model will be returned.
Push notification on new contact joined
There is a way to get a push notification when some contact from your Address Book has been registered in the app. You can enable this feature at QuickBlox Dashboard. Just follow Dashboard => YOUR_APP => Users => Settings direction and enable push notifications for new contacts.
Updated about 2 years ago