PDA

View Full Version : Accessing Address Book in iPhone SDK?




jhershauer
Mar 7, 2008, 11:28 PM
Hi,
I've created a simple list application, starting out with a custom datasource class that simply returns a hard-wired list of data. With that working, I'm now attempting to alter my datasource class to pull data from the address book. I've added a reference to AddressBook/ABAddressBook.h and created an ABAddressBookRef variable. The application compiles fine with that, and I'm able to get Code Sense into the ABAddressBook API. However, if I attempt to initialize my ABAddressBookRef variable by calling ABAddressBookCreate(), I get a link error like this:

"_ABAddressBookCreate", referenced from:
-[TestTableDataSource init] in TestTableDataSource.o
symbol(s) not found
collect2: id returned 1 exit status

I'm new to this environment, so there may be something obvious I'm missing here in terms of setting this up. Has anybody successfully initialized/accessed the Address Book API with the iPhone SDK yet? Any quick samples of how to get the ball rolling with that API?

Thanks,
Jeff



kainjow
Mar 7, 2008, 11:33 PM
Did you link in the entire framework?

jhershauer
Mar 7, 2008, 11:52 PM
Did you link in the entire framework?

Wow...quick response! I was just coming back to answer myself. No, I hadn't linked in that framework. I was just poking around and noticed the "Linked Frameworks" node in the Groups and Files pane. All is well now.

Thanks!

Jeff

jhershauer
Mar 8, 2008, 12:46 PM
OK, I've got a follow-up question that's more of a beginner Objective-C question:

I wanted to create a couple of class-level variables ("ivars" I suppose they're called). One would be a pointer to the AddressBook itself, and the other would point to the array of all people in the Address Book (NSArray *). This was so I wouldn't have to keep re-getting these things in the other methods of my class.

I was looking for the equivalent of a "constructor" in Objective-C and found the -init() method, so I overrode that as follows:

-(id)init
{
self = [super init];
addressBook = <SDKMehtodToGetAddressBook>();
allPeople = (NSArray *)<SDKMethodToGetArrayOfPeople>(addressBook);
return self;
}

This didn't work. When I got into my other methods, addressBook and allPeople were always nil. I got around this by turning addressBook and allPeople into properties, and implementing getters like this:


- (<SDKTypeThatReferencesAnAddressBook>)_getAddressBook {
if (nil == addressBook)
{
addressBook = <SDKMethodToReturnAnAddressBook>();
}
return addressBook;
}
- (NSArray *)_getAllPeople {
if (nil == allPeople)
{
allPeople = (NSArray *)<SDKMethodToGetArrayOfPeople>(self.addressBook);
}
return allPeople;
}


This works fine, and my application is happily running in the simulator, returning a list of people from the address book. However, I'm curious as to why the assignments to the allPeople and addressBook variables didn't "stick" when I tried to do it through the init method. I've done some google searching, and that seems like it's supposed to be the appropriate place to initialize ivars for the class. Any insight would be appreciated.


Here's the interface for my class:

#import <SDK UI Kit>
#import <SDK Address Book>


@interface TestTableDataSource : NSObject <UITableViewDataSource, UITableViewDelegate> {
<SDKAddressBookReferenceType> addressBook;
NSArray *allPeople;
}

@property (getter=_getAddressBook,assign) <SDKTypeThatReferencesAnAddressBook> addressBook;
@property (getter=_getAllPeople,assign) NSArray *allPeople;
- (NSString *)stringValueForRow:(NSInteger)row;

@end

Thanks,
Jeff

[Edit]: Hmm...I see other references to NDA, and although this thing is publicly available now, I know I agreed to something (didn't actually read it) when I downloaded the SDK, so I've gone back and attempted to kind of obfuscate the code so as not to give any SDK "secrets" away. :-)

kainjow
Mar 8, 2008, 12:53 PM
Your init method may not be getting called. You can NSLog() it to see if it is.

How is your class being created?

jhershauer
Mar 8, 2008, 01:50 PM
Your init method may not be getting called. You can NSLog() it to see if it is.

How is your class being created?

Doh! I feel like an idiot. You're right...the init wasn't being called. I could have sworn I put a breakpoint in my init method to make sure the variables were getting set, but I must not have (taking in a lot of new stuff all at once, and probably getting a bit discombobulated).

Your tip was enough to point me to my problem. I was creating my class instance like this:


//Set up data source
testDs = [TestTableDataSource alloc];

Now I'm creating it like this, and it's working:


//Set up data source
testDs = [[TestTableDataSource alloc] init];

I was assuming that simply doing an alloc on the class was the equivalent of doing a New in C#, and thought my init (which I was thinking of as a constructor) would automatically be called.

At least I learned how to create custom property accessors in the process. :-)

Thanks again!

Jeff

nick718
Mar 11, 2008, 08:51 PM
Hey, I'm trying to do something similar. How did you find usages for the ABAddressBookRef class? I looked at the library ref on the developer center, but it had no example code and nothing about how to create the instance. I feel like I must be missing a large amount of reference documentation somewhere.

Thanks

netik
Mar 12, 2008, 01:31 AM
Hey, I'm trying to do something similar. How did you find usages for the ABAddressBookRef class? I looked at the library ref on the developer center, but it had no example code and nothing about how to create the instance. I feel like I must be missing a large amount of reference documentation somewhere.

Thanks

All of the reference documentation is available in the research assistant, and apple has squirreled away tons of example code inside of the documentation bundle. Go to Help, and then "Show research assistant".

Search in the SDK and you should be set.

netik
Mar 12, 2008, 01:32 AM
Doh! I feel like an idiot. You're right...the init wasn't being called. I could have sworn I put a breakpoint in my init method to make sure the variables were getting set, but I must not have (taking in a lot of new stuff all at once, and probably getting a bit discombobulated).


I have a similar problem to yours, but it seems that the compiler is not finding any of the header files. I have errors like:


/Users/jna/Documents/Iphone App Dev/WhatHappened/Classes/../RootViewController.m:194: error: 'ABPerson' undeclared (first use in this function)
/Users/jna/Documents/Iphone App Dev/WhatHappened/Classes/../RootViewController.m:194: error: 'aPerson' undeclared (first use in this function)
/Users/jna/Documents/Iphone App Dev/WhatHappened/Classes/../RootViewController.m:194: error: 'ABAddressBook' undeclared (first use in this function)
/Users/jna/Documents/Iphone App Dev/WhatHappened/Classes/../RootViewController.m:195: error: 'ABMutableMultiValue' undeclared (first use in this function)
/Users/jna/Documents/Iphone App Dev/WhatHappened/Classes/../RootViewController.m:195: error: 'anAddressList' undeclared (first use in this function)
/Users/jna/Documents/Iphone App Dev/WhatHappened/Classes/../RootViewController.m:196: error: 'kABAddressProperty' undeclared (first use in this function)
/Users/jna/Documents/Iphone App Dev/WhatHappened/Classes/../RootViewController.m:198: warning: unused variable 'primaryIndex'


and I have certainly put the framework in both the 'link binary with libraries' part of my target, as well as in the 'linked framework' part. What gives?

Why can't it see the header files?!

nick718
Mar 12, 2008, 07:46 AM
Thanks for the info.

I am still having an issue though. I show the Research Assistant, but mo matter what I click on, even non-iPhone SDK classes, I see nothing in the window. Is there something I'm missing? I tried just putting the cursor over the class name in the code, and also highlighting the name.

Thanks

kainjow
Mar 12, 2008, 09:09 AM
and I have certainly put the framework in both the 'link binary with libraries' part of my target, as well as in the 'linked framework' part. What gives?

Why can't it see the header files?!

Did you #import <AddressBook/AddressBook.h> ?

nick718
Mar 12, 2008, 06:35 PM
So I've finally managed to get the Research Assistant working, but now I have a new question. This may be a dumb question but, how did you manage to get a list of all the records in the address book? In looking at the doc and the AddressBookh file, I see no functions for just getting a or all of the records.

Thanks!

jhershauer
Mar 12, 2008, 07:02 PM
So I've finally managed to get the Research Assistant working, but now I have a new question. This may be a dumb question but, how did you manage to get a list of all the records in the address book? In looking at the doc and the AddressBookh file, I see no functions for just getting a or all of the records.

Thanks!

For that, you'll want to import the ABPerson header file and take a look at the available functions there. I believe it's also documented. There's an ABAddressBookCopyArrayOfAllPeople function that will get you all of the records. You'll need your address book reference as a parameter for the ABPerson functions.

Jeff

nick718
Mar 12, 2008, 08:43 PM
Awesome, thanks so much!

One more question to anybody who can answer. Am I missing something here, or are all the AddressBook libraries standard C libraries? I'm finding myself relying on all of these C functions that are passed object references and parameters, it seems to defeat the idea of using Objective C as an OO language for the SDK. Are there some sort of OC wrappers for all this functionality? Or are some of the APIs just standard C, no if an or buts?

Thanks,
Nick

netik
Mar 13, 2008, 03:37 AM
Did you #import <AddressBook/AddressBook.h> ?

Ahh, it was getting the headers but I was used to addressbook in cocoa and accessing it the wrong way.

The iPhone SDK is a bit different, so, to open the address book and get the contacts, it looks something like this:


// open the default address book.
ABAddressBookRef m_addressbook = ABAddressBookCreate();
if (!m_addressbook) {
NSLog(@"opening address book");
}

// can be cast to NSArray, toll-free
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);

// CFStrings can be cast to NSString!

for (int i=0;i < nPeople;i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
... do something with ABRecordRef...
}

[region release];

netik
Mar 13, 2008, 03:39 AM
For that, you'll want to import the ABPerson header file and take a look at the available functions there. I believe it's also documented. There's an ABAddressBookCopyArrayOfAllPeople function that will get you all of the records. You'll need your address book reference as a parameter for the ABPerson functions.

Jeff

Importing <AddressBook/AddressBook.h> all of the other header files for you. You shouldn't import ABPerson.h directly.

nick718
Mar 13, 2008, 07:26 AM
Ahh, it was getting the headers but I was used to addressbook in cocoa and accessing it the wrong way.

The iPhone SDK is a bit different, so, to open the address book and get the contacts, it looks something like this:


// open the default address book.
ABAddressBookRef m_addressbook = ABAddressBookCreate();
if (!m_addressbook) {
NSLog(@"opening address book");
}

// can be cast to NSArray, toll-free
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);

// CFStrings can be cast to NSString!

for (int i=0;i < nPeople;i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
... do something with ABRecordRef...
}

[region release];


Reassuring to see that, it's almost exactly what I have. Thanks for the tip on the casting though, I didn't realize those foundations classes could be cast into NS classes that easily.

Do you know how I could go about getting which indexes are for which Record properties? It seems like there should be a list somewhere, but I could not find one.
Thanks

drivefast
Mar 13, 2008, 02:18 PM
Do you know how I could go about getting which indexes are for which Record properties? It seems like there should be a list somewhere, but I could not find one.
i think youre talking about how to get the values in the database fields...? something like this?

NSString *contactFirstLast = [NSString stringWithFormat:@"%@,%@",
ABRecordCopyValue(ref, kABPersonFirstNameProperty),
ABRecordCopyValue(ref, kABPersonLastNameProperty)
];

the kABPersonBlahBlahProperty constants (or enums, i forgot what they are) are decribed in the ABPerson documentation pages. it's a little more difficult to get a hold of the multivalues, like addresses and phone numbers, ive got something working if you need it, but i feel it's not as nice as it could be.

kainjow
Mar 13, 2008, 02:29 PM
Any values used from ABRecordCopyValue() need to be released when done, so that code above is leaking memory, and if you use that in a loop with every contact, that'd be a lot of memory to leak.

It should probably be something like this:

CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
NSString *contactFirstLast = [NSString stringWithFormat:@"%@,%@",
firstName, lastName];
CFRelease(firstName);
CFRelease(lastName);

drivefast
Mar 13, 2008, 02:55 PM
thanks for pointing that out. i should have said that in my previous post - i only started with obj-c a couple of days ago ;)

now anyone has any idea how to get the own phone's phone number?... on the iPhone it shows up on top of the "all contacts" list, but i couldnt find it anywhere in the address book database.

gifford
Mar 13, 2008, 02:58 PM
I have been trundling through the adressbook iphone framework all day looking for ways to receive "missed call" events, are an array of missed calls, or any info to do with missed calls.

Has anyone spotted any info on this anywhere?

netik
Mar 14, 2008, 03:34 AM
Any values used from ABRecordCopyValue() need to be released when done, so that code above is leaking memory, and if you use that in a loop with every contact, that'd be a lot of memory to leak.

It should probably be something like this:

CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
NSString *contactFirstLast = [NSString stringWithFormat:@"%@,%@",
firstName, lastName];
CFRelease(firstName);
CFRelease(lastName);

I really don't have my head completely around references and what needs to be released and what doesn't yet.

I don't understand how you can copy something into a reference, and then release it, and still be able to use it. Got a reference for this that I can read?

netik
Mar 14, 2008, 03:37 AM
I have been trundling through the adressbook iphone framework all day looking for ways to receive "missed call" events, are an array of missed calls, or any info to do with missed calls.

Has anyone spotted any info on this anywhere?

I don't think you're going to get that from the address book.

There's no section of the SDK that I can find to get to call, calendar, or voice mail data, so I guess that's not available just yet. Maybe there's an NSDictionary someplace with this?

gifford
Mar 14, 2008, 05:19 AM
I don't think you're going to get that from the address book.

There's no section of the SDK that I can find to get to call, calendar, or voice mail data, so I guess that's not available just yet. Maybe there's an NSDictionary someplace with this?

I was told by one of the guy's from Apple that address book was where to look. So I kept on looking, and looking, and looking, and now I have given up.

I will have to wait I suppose, the application I want it for has already been put off for 3 years in anticipation of the iphone (and lack of patience to learn any other inevitably doomed mobile platform) so whats another couple of years gona make!

kainjow
Mar 14, 2008, 09:40 AM
I really don't have my head completely around references and what needs to be released and what doesn't yet.

I don't understand how you can copy something into a reference, and then release it, and still be able to use it. Got a reference for this that I can read?

Read up on memory management: http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html

cmaier
Mar 14, 2008, 09:49 AM
Remember that "release" isn't like "delete" in C++ or free in C. It merely reduces the reference count.

muscheteer
Mar 19, 2008, 03:56 AM
I have been trundling through the adressbook iphone framework all day looking for ways to receive "missed call" events, are an array of missed calls, or any info to do with missed calls.

Has anyone spotted any info on this anywhere?

Has anyone try to handle a call event or someting like this ?

Thanks

kiezarsoze
Mar 23, 2008, 08:43 PM
I've gotten this far, but when I try to use the property called kABPersonPhoneProperty in ABPerson.h I get a return value of NSCFType.

At loggerheads. Can anyone show me how to get the phone numbers, specifically the cell phone number (kABPersonPhoneMobileLabel)?

(Also trying to get the phone number of the device itself)

i think youre talking about how to get the values in the database fields...? something like this?

NSString *contactFirstLast = [NSString stringWithFormat:@"%@,%@",
ABRecordCopyValue(ref, kABPersonFirstNameProperty),
ABRecordCopyValue(ref, kABPersonLastNameProperty)
];

the kABPersonBlahBlahProperty constants (or enums, i forgot what they are) are decribed in the ABPerson documentation pages. it's a little more difficult to get a hold of the multivalues, like addresses and phone numbers, ive got something working if you need it, but i feel it's not as nice as it could be.

kiezarsoze
Mar 24, 2008, 01:08 AM
anyone got experience with ABMultiValueRef and ABMultiValueIdentifier. How do we initialize these variables? How can we use them to access multi value strings such as phone numbers and other contact info in the iphone addressbook sdk?

yarshure
Apr 7, 2008, 09:34 PM
use this code get person phone number
ABAddressBookRef book = ABAddressBookCreate(); // b
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(book);

NSLog(@"%@",ABMultiValueCopyValueAtIndex(ABRecordCopyValue(CFArrayGetValueAtIndex(people,indexPath.row),kABPers onPhoneProperty) ,0));

anyone got experience with ABMultiValueRef and ABMultiValueIdentifier. How do we initialize these variables? How can we use them to access multi value strings such as phone numbers and other contact info in the iphone addressbook sdk?

StrAbZ
Apr 21, 2008, 11:42 AM
Thank you for all the answers in this thread, it was very helpful !

I've tried to use the SimpleDrillDown example from the apple dev center, and replace the list of name define in the code by the name from the address book, but as you can imagine i have a probleme.

This is the code i use to retrieve data from the addressBook.

ABAddressBookRef addressBook = ABAddressBookCreate();

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

NSMutableArray *masterList = [[NSMutableArray alloc] init];
for (int i = 0; i < nPeople; i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
NSString *contactFirstLast = [NSString stringWithFormat: @"%@", (NSString *)lastName];
CFRelease(firstName);
CFRelease(lastName);
[masterList addObject:contactFirstLast];
[contactFirstLast release];
}

self.list = masterList;
[masterList release];


the problem is : if i wrote the ligne :
NSString *contactFirstLast = (NSString *)lastName;
I can get all lastName, but when i try to use :
NSString *contactFirstLast = [NSString stringWithFormat: @"%@", (NSString *)lastName];
it doesn't work, did you have any idea of the probleme?

It look likes [NSString stringWithFormat: @"LN: %@", (NSString *)lastName] and (NSString *)lastName are not the same kind of NSString... i really can't understand...

Thanks

MediadorAv
May 28, 2008, 11:58 AM
i'm new in the iPhone, somebody know how add a record in the addressBook. thanks

gnasher729
May 28, 2008, 01:04 PM
[Edit]: Hmm...I see other references to NDA, and although this thing is publicly available now, I know I agreed to something (didn't actually read it) when I downloaded the SDK, so I've gone back and attempted to kind of obfuscate the code so as not to give any SDK "secrets" away. :-)

The iPhone SDK _is_ under NDA and it is _not_ publicly available. And yes, you did agree not to publish any information about the iPhone SDK at all.

MediadorAv
May 28, 2008, 04:22 PM
oooh ok, no problem, I'll search other site,or anywhere, with a person like you the world don't work, so, anyway I will found it, and I'll publish all, that it is not problem for me.jajaja :D secrets (like secrets expedients "X").

MediadorAv
Jun 2, 2008, 01:12 PM
this is for add record to addressBook,

-(void)editar:(id)sender{

ABAddressBookRef libroDirec = ABAddressBookCreate();
ABRecordRef persona = ABPersonCreate();

ABRecordSetValue(persona, kABPersonFirstNameProperty, @"kate" , nil);
ABRecordSetValue(persona, kABPersonLastNameProperty, @"Hutson", nil);
ABAddressBookAddRecord(libroDirec, persona, nil);
ABAddressBookSave(libroDirec, nil);

CFRelease(persona);
}



this code is for remove one record in the addressbook like the record in this case with the name kate. good luck.

-(void)editar:(id)sender
{

ABAddressBookRef libroDirec = ABAddressBookCreate();

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(libroDirec);

CFIndex xPeople = ABAddressBookGetPersonCount(libroDirec);


for (int i=0; i <xPeople; i++ )
{
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
CFStringRef nombreX = ABRecordCopyValue(ref, kABPersonFirstNameProperty);


CFStringRef cadena = CFSTR("kate");



if (CFStringCompare(nombreX, cadena , 0) == kCFCompareEqualTo )
{
ABAddressBookRemoveRecord(libroDirec, ref, nil);
ABAddressBookSave(libroDirec, nil);

}



}


hi, hi, :D secrets,
don't more tv, get down of the cloud programmers, we share, we aren't FBI agents, or anything else like secrets.

sabross
Jun 3, 2008, 06:08 PM
Did you link in the entire framework?

Hi I'm having the same problem, and I linked the framework using Ctrl-Click Frameworks>add>existing framework and choosing "AddressBook Framework"

I add #import <AddressBook/AddressBook.h> and then if i try

ABAddressBook *ab = [ABAddressBook sharedAddressBook];

I get the error:
ABAddressBook undeclared

but no error in the "import" line.

What am I missing to link correctly the library??

Thx,

Dani

MediadorAv
Jun 3, 2008, 07:09 PM
hi, you have problem because in the iPhone is diferent like this:

addressBookRef ab=AddressBookCreate();

in the example above you see something like this

korzieher
Jun 4, 2008, 03:31 AM
hi there

has anyone ever managed to add an ABMutableMultiValue to an ABRecord?

what i'm trying to do is to add a new record to the addressbook.
i managed to add a record with several properties as described earlier in this thread. but when it comes to multivalues i don't have a clue...
basically i want to add for example an address. i noticed that there are constants (keys) like 'kABPersonAddressZIPKey' and a 'kABPersonAddressProperty'. but how can i add the zip key to the record?

thanks for any help.

now i came up with this.

current = ABPersonCreate();
ABRecordSetValue(current, kABPersonFirstNameProperty, @"foo", nil);
ABRecordSetValue(current, kABPersonLastNameProperty, @"bar", nil);

ABMutableMultiValueRef address = ABMultiValueCreateMutable(kABPersonAddressProperty);
if(!ABMultiValueAddValueAndLabel(address, @"street 123", kABPersonAddressStreetKey, nil))
NSLog(@"adding value/label didn't work.");

if(!ABRecordSetValue(current, kABPersonAddressProperty, address, nil))
NSLog(@"setting value didn't work.");

if(!ABAddressBookAddRecord(addressBook, current, nil))
NSLog(@"adding didn't work.");

ABAddressBookSave(addressBook, nil);


but it crashes on the last line with the messages

*** -[NSCFString count]: unrecognized selector sent to instance 0x63e0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString count]: unrecognized selector sent to instance 0x63e0'

pranav
Apr 29, 2009, 05:54 AM
contactFirstLast is autorelased object, so you should not release it.

Thank you for all the answers in this thread,
it was very helpful !

I've tried to use the SimpleDrillDown example from the apple dev center, and replace the list of name define in the code by the name from the address book, but as you can imagine i have a probleme.

This is the code i use to retrieve data from the addressBook.

ABAddressBookRef addressBook = ABAddressBookCreate();

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

NSMutableArray *masterList = [[NSMutableArray alloc] init];
for (int i = 0; i < nPeople; i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);
NSString *contactFirstLast = [NSString stringWithFormat: @"%@", (NSString *)lastName];
CFRelease(firstName);
CFRelease(lastName);
[masterList addObject:contactFirstLast];
[contactFirstLast release];
}

self.list = masterList;
[masterList release];


the problem is : if i wrote the ligne :
NSString *contactFirstLast = (NSString *)lastName;
I can get all lastName, but when i try to use :
NSString *contactFirstLast = [NSString stringWithFormat: @"%@", (NSString *)lastName];
it doesn't work, did you have any idea of the probleme?

It look likes [NSString stringWithFormat: @"LN: %@", (NSString *)lastName] and (NSString *)lastName are not the same kind of NSString... i really can't understand...

Thanks

StrAbZ
Apr 30, 2009, 05:10 AM
It was a years ago (starting objective c), but thx for replying ^^
I learned a lot since that post :D

geckus
Jan 17, 2010, 12:40 PM
NSMutableArray *phones = [[NSMutableArray alloc] init];//define array geckus
CFStringRef phoneNumber;
for (int i=0;i < nPeople;i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);

ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(ref, kABPersonPhoneProperty);

NSString *phoneNumber = ABMultiValueCopyValueAtIndex(phoneMulti, i);

NSString *contactFirstLast = [NSString stringWithFormat:@"%@,%@",
ABRecordCopyValue(ref, kABPersonFirstNameProperty),
phoneNumber
];




printf("%s\n", [contactFirstLast UTF8String]);
//NSLog(mobileNo);
CFRelease(phoneNumber);
}


wonder what im doing wrong? any help would be good thanks

StrAbZ
Jan 18, 2010, 05:50 AM
From what i see, your error is here :

NSString *phoneNumber = ABMultiValueCopyValueAtIndex(phoneMulti, i);

your i var correspond to the address book index, not the multivalue index, you must iterate over the multivalue, you get the count with ABMultiValueGetCount();

geckus
Jan 18, 2010, 09:57 AM
pasting the whole function
- (void)constructTableGroups
{
//const NSInteger NUM_ROWS = 20;

//geckus start
// open the default address book.
ABAddressBookRef m_addressbook = ABAddressBookCreate();
if (!m_addressbook) {
NSLog(@"opening address book");
}

// can be cast to NSArray, toll-free
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);

// CFStrings can be cast to NSString!
NSMutableArray *selectableRows = [NSMutableArray array];//define array for table geckus

NSMutableArray *phones = [[NSMutableArray alloc] init];//define array geckus
CFStringRef phoneNumber;
for (int i=0;i < nPeople;i++) {
ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
//... do something with ABRecordRef...
ABMutableMultiValueRef phoneMulti = ABRecordCopyValue(ref, kABPersonPhoneProperty);

NSString *phoneNumber = ABMultiValueCopyValueAtIndex(phoneMulti, i);

NSString *contactFirstLast = [NSString stringWithFormat:@"%@,%@",
ABRecordCopyValue(ref, kABPersonFirstNameProperty),
phoneNumber
];




printf("%s\n", [contactFirstLast UTF8String]);
//NSLog(mobileNo);


[selectableRows addObject:
[[[MultiSelectCellController alloc]
initWithLabel:[NSString stringWithFormat:contactFirstLast, i]]
autorelease]];//geckus put var into array
CFRelease(phoneNumber);
}
tableGroups = [[NSArray alloc] initWithObjects:selectableRows, nil];//init object geckus...not sure yet
//geckus end

}

Im really new would you be able to help me correct the codes.
all i want to do is draw addressbook (name,phone) into a tableview ..been banging my head against this code for a few days already .