You Are Here:

Community: Wiki

This page was last modified on 1 September 2009, at 09:58.

How to add contacts from phonebook

From Forum Nokia Wiki

API is deprecated in S60 3.2 so while using this API do consider this

There are plenty of occasions when you need to add contacts from Phonebook to your Symbian C++ application. Examples are SMS, Call Control applications etc. To do that you have to first load the phonebook's resource file like

RPbkViewResourceFile phonebookResource( *(CEikonEnv::Static()) );
phonebookResource.OpenL();

In general you would want to allow users to fetch multiple entries from the phonebook dialog so you can use CPbkMultipleEntryFetchDlg and pass the correct parameters. Also attach the correct Phonebook Contact Engine to the dialog. (Here I am picking the default contact book, whereas you may want to display your own special contact book database.) The code may look like

CPbkMultipleEntryFetchDlg::TParams params;
CleanupStack::PushL(params);
 
CPbkContactEngine* iPbkContactEngine =
CPbkContactEngine::NewL(&iEikonEnv->FsSession());
CleanupStack::PushL(iPbkContactEngine);
params.iContactView = &iPbkContactEngine->AllContactsView();
 
CPbkMultipleEntryFetchDlg* fetcher =
CPbkMultipleEntryFetchDlg::NewL(params, *iPbkContactEngine);
fetcher->SetMopParent(this);

Later just execute the dialog. On return, the dialog will give you a list of contact ids with which you can access individually selected contacts from Contact Engine and process them

TBuf<30> phoneNumber;
 
TInt paramCount = params.iMarkedEntries->Count();
// Get the selected contacts id array
for ( TInt i = 0; i < paramCount; ++i )
{
const TContactItemId cid = ( *params.iMarkedEntries )[i];
// Open the selected contact using Phonebook engine,
// choose correct number (launch list query if needed)
CPbkContactItem* pbkItem = iPbkContactEngine->ReadContactLC( cid );
TPbkContactItemField* tmp;
if ((tmp = pbkItem->FindField(EPbkFieldIdPhoneNumberMobile)) != NULL)
{
phoneNumber = tmp->Text();
if(phoneNumber.Length() > 0){
if(iData)
iAppView->AddNumber(phoneNumber);
}
}
 
CleanupStack::PopAndDestroy(1);//CPbkContactItem
}

If you want to implement the behavior that, if a contact item has more than one number, and you want to show native dialog to choose between the numbers(like in native Sms application), you can use following code

TBuf<30> phoneNumber;
 
TInt paramCount = params.iMarkedEntries->Count();
// Get the selected contacts id array
for ( TInt i = 0; i < paramCount; ++i )
{
TBuf<128> phoneNumber;
TBuf<128> phoneName;
TBool namefound = EFalse;
const TContactItemId cid = ( *params.iMarkedEntries )[i];
// Open the selected contact using Phonebook engine,
// choose correct number (launch list query if needed)
CPbkContactItem* pbkItem = iPbkContactEngine->ReadContactLC( cid );
//Using CPbkSmsAddressSelect dialog for user to choose between numbers
CPbkSmsAddressSelect* dlg = new (ELeave) CPbkSmsAddressSelect();
CPbkSmsAddressSelect::TParams addparams(*pbkItem);
TBool selected = dlg->ExecuteLD(addparams);
//If selected is ETrue, it means that user has selected some number, else user has pressed cancel
//Also, if contact item has only one number, then there is no popup shown to the user and returned value is ETrue
if(selected){
//Getting the selected field, i.e. Mobile, Mobile (Business),Tel, Tel. (Home)
//You can find the exact label of field by numtmp->Label()
const TPbkContactItemField* numtmp = addparams.SelectedField();
if(numtmp){
phoneNumber = numtmp->Text();
}
TPbkContactItemField* nametmp;
if ((nametmp = pbkItem->FindField(EPbkFieldIdFirstName)) != NULL)
{
phoneName = nametmp->Text();
if(phoneName.Length() > 0){
namefound = ETrue;
}
}
if ((nametmp = pbkItem->FindField(EPbkFieldIdLastName)) != NULL)
{
if(namefound){
phoneName.Append(_L(" "));
phoneName.Append(nametmp->Text());
}
else{
phoneName = nametmp->Text();
}
}
}
CleanupStack::PopAndDestroy(1);//CPbkContactItem
}

In the end clean up your stack and close phonebook resource

//CPbkContactEngine, CPbkMultipleEntryFetchDlg::TParams
CleanupStack::PopAndDestroy(2);
phonebookResource.Close();

Headers required

#include <rpbkviewresourcefile.h>
#include <cpbkmultipleentryfetchdlg.h>
#include <cpbkcontactengine.h>
#include <cpbkcontactitem.h>
#include <cpbksmsaddressselect.h>

Library needed

LIBRARY   pbkview.lib pbkeng.lib

Capability required

Capability  ReadDeviceData ReadUserData WriteDeviceData WriteUserData

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fCS000883E5fE2dE5fPausingE5fanE5fapplicationE5fonE5fanE5fincomingE5fSMSX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxCS000883E20E2dE20PausingE20anE20applicationE20onE20anE20incomingE20SMSE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtopicQUqfnTopicZmessagingQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d02X qfnZuserE5ftagQSxmessagingX qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx4E2E0000X