Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Retrieving phone number from address book,

satvik_85satvik_85 Posts: 23Registered Users
edited January 2010 in iPhone SDK Development
Hi guys,

I'm facing a problem in retrieving fone number of AbRecordRef. Its having the kABPersonPhoneMobileLabel which is CFStringRef something. The function "ABRecordCopyValue" is for record and abpropertyid. Not for record and cfstringref

How can i get the phone number and store it....

Thanks all,

Satvik
Post edited by satvik_85 on

Replies

  • uprise78uprise78 Posts: 159Registered Users
    edited July 2008
    The phone numbers are ABMultiValueRefs and are a bit tricky at first to get information from. Below is a function i wrote to pull out data from multi vals.
    - (NSMutableArray*)extractDataFromABMultiValueRef:(ABMultiValueRef)multiValueRef
    {
    	NSMutableArray *data = [[[NSMutableArray alloc] init] autorelease];
    
    	for(int i = 0; i < ABMultiValueGetCount(multiValueRef); i++)
    	{
    		NSString *label = [self extractAndCleanPropertyLabel:(NSString*)ABMultiValueCopyLabelAtIndex(multiValueRef, i)];
    		NSString *value = [self cleanPhoneNumberString:(NSString*)ABMultiValueCopyValueAtIndex(multiValueRef, i)];
    
    		LabelValueObject *labelVO = [[LabelValueObject alloc] initWithLabel:label value:value];
    		[data addObject:labelVO];
    		[labelVO release];
    	}
    
    	return data;
    }
    
  • vosensvosens Posts: 10Registered Users
    edited August 2008
    could you post example code for how you would use that method to print a contact's street address to a label?

    I can't quite make out how your method is working.
  • angadskangadsk Posts: 6New Users
    edited August 2008
    I guess i can help u in this...
    here is the code...
    This is how i did....
    there might be some other method too ..


    ABMultiValueRef name1 =(NSString*)ABRecordCopyValue(person,kABPersonPhoneProperty);
    NSString* mobile=@"";
    NSString* mobileLabel;
    for(CFIndex i=0;i<ABMultiValueGetCount(name1);i++)
    {
    mobileLabel=(NSString*)ABMultiValueCopyLabelAtIndex(name1, i);
    if([mobileLabel isEqualTo:@"_$!<Mobile>!$_"])
    {
    mobile=(NSString*)ABMultiValueCopyValueAtIndex(name1,i);

    }
    }


    I guess this might help you ... .

    Regards
    Angad
  • vosensvosens Posts: 10Registered Users
    edited August 2008
    Thanks a lot Angad! I don't know why I find it so much more difficult to deal with the MultiValue properties than the others. I really appreciate you posting a reply so fast, I think this is just what I needed.

    To use this code for a contact's Street address would I just change kABPersonPhoneProperty to kABPersonAddressStreetKey? I'll tweak around with it today.

    Thanks again for your help.
  • angadskangadsk Posts: 6New Users
    edited August 2008
    Hi vosens,

    The coding changes according to the users requirement.. so in order to get the values from the address book and all we have to get the values according to the property. so we have to use kABPersonAddressProperty. This will extract all the values from the address book i.e is the home address as well as the work address.

    Here is the code that you want ...
    This is for getting the Home Address ... If you want the Work address the you have to change the if condition to

    // if([HomeLabel isEqualTo:@"_$!<Work>!$_"])

    // Code.........

    NSArray *allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);

    int i;
    for (i = 0; i < [allPeople count]; i++) {
    ABRecordRef record = [allPeople objectAtIndex:i];

    ABMutableMultiValueRef multiValue = ABRecordCopyValue(record, kABPersonAddressProperty);
    for(CFIndex i=0;i<ABMultiValueGetCount(multiValue);i++)
    {
    NSString* HomeLabel=(NSString*)ABMultiValueCopyLabelAtInde x(multiValue, i);
    if([HomeLabel isEqualTo:@"_$!<Home>!$_"])
    {

    CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(multiValue, i);
    CFStringRef street = CFDictionaryGetValue(dict, kABPersonAddressStreetKey);
    CFStringRef city = CFDictionaryGetValue(dict, kABPersonAddressCityKey);
    CFStringRef country = CFDictionaryGetValue(dict, kABPersonAddressCountryKey);
    CFRelease(dict);

    NSString *syntheticAddress = [NSString stringWithFormat:@"%@, %@, %@",street,city,country];
    }

    }


    I Guess this should pretty well work for you... Hope this helps you . Please do let me know about it ..

    Cheers
    Angad
  • vosensvosens Posts: 10Registered Users
    edited August 2008
    Works like a charm! You are the man!!

    Thanks a million.
  • bergetunbergetun Posts: 31Registered Users
    edited August 2008
    Thanks for this great thread.

    Does anyone have code example of creating a new contact with some contact details.
  • angadskangadsk Posts: 6New Users
    edited August 2008
    Hi bergetun,

    Apple - Support - Discussions - iPhone - kABPersonAddressProperty crash ...

    You can go through this thread. Last post..
    This might help you..

    Cheers
    Angad
  • tobiasbtobiasb Posts: 4New Users
    edited September 2008
    angadsk wrote: »
    I guess i can help u in this...
    here is the code...
    This is how i did....
    there might be some other method too ..


    ABMultiValueRef name1 =(NSString*)ABRecordCopyValue(person,kABPersonPhoneProperty);
    NSString* mobile=@"";
    NSString* mobileLabel;
    for(CFIndex i=0;i<ABMultiValueGetCount(name1);i++)
    {
    mobileLabel=(NSString*)ABMultiValueCopyLabelAtIndex(name1, i);
    if([mobileLabel isEqualTo:@"_$!<Mobile>!$_"])
    {
    mobile=(NSString*)ABMultiValueCopyValueAtIndex(name1,i);

    }
    }


    Wow great, this is exactly what i need, and it runs fine in simulator, but it crashes executed on my iPhone...

    Debugger is showing this, Problem seems to be the isEqualto Part on the loop-head.
    2008-09-02 11:44:13.910 NetMsg[283:20b] *** -[NSCFString isEqualTo:]: unrecognized selector sent to instance 0x1a5a60
    2008-09-02 11:44:13.921 NetMsg[283:20b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFString isEqualTo:]: unrecognized selector sent to instance 0x1a5a60'
    

    Any Ideas to get this running on the Device?
  • tobiasbtobiasb Posts: 4New Users
    edited September 2008
    okay found it bymyself. damn objective-c basics;) should buy myself a book.

    replace
    if([mobileLabel isEqualTo:@"_$!<Mobile>!$_"])
    

    with
    if([mobileLabel isEqualToString:@"_$!<Mobile>!$_"])
    

    and it will run on Device as well.
  • tobiasbtobiasb Posts: 4New Users
    edited September 2008
    next problem with this chunk is that it wont find the mobile number if its the first one..grr..

    any suggestions?
  • pankajmittalpankajmittal Posts: 2New Users
    edited October 2008
    Hiii,

    I'm trying to retrieve email addrs by following code

    address_book = ABAddressBookCreate();
    address_cf = ABAddressBookCopyArrayOfAllPeople(address_book);
    CFIndex np = ABAddressBookGetPersonCount(address_book);

    int i;
    for (i = 0; i < np; i++)
    {
    ABRecordRef record = CFArrayGetValueAtIndex(address_cf, i);
    CFStringRef email1 = ABRecordCopyValue(record, kABPersonEmailProperty);
    NSString *temp = [[NSString alloc] initWithFormat:@"%@",email1];
    NSLog(@"emails %@",temp);
    }

    but output email I'm getting is:
    <NSCFType: 0x10aed90>

    Can you help me regarding this.....
  • javid.alimohideenjavid.alimohideen Posts: 298Registered Users
    edited October 2008
    Hey Pankaj,
    The Mail addresses are AbMultiValueRef, so your code would be someting like below
    ABRecordRed person = (ABRecordRef *) CFArrayGetValueAtIndex(address, i);
    ABMultiValueRef emails = (ABMultiValueRef) ABRecordCopyValue(person, kABPersonEmailProperty);
    NSString *emailID = (NSString *)ABMultiValueCopyValueAtIndex(emails, 0);
    
    [emailD release];
    [emails release];
    
  • pankajmittalpankajmittal Posts: 2New Users
    edited October 2008
  • cjpearlcjpearl Posts: 111Registered Users
    edited January 2009
    I am having endless problems with this class. I just want to pull a mobile number out of the contact list, and stick it into a UITextField.
    ABMultiValueRef phoneNums = (ABMultiValueRef)ABRecordCopyValue(person, kABPersonPhoneProperty);
    	NSString *phoneNum = nil;
    	NSString *searchString = nil;
    	for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNums); i++) {
    		searchString = (NSString *)ABMultiValueCopyLabelAtIndex(phoneNums, i);
    		if([searchString isEqualToString:@"_$!<Mobile>!$_"]) {
    			i = ABMultiValueGetCount(phoneNums);
    			NSLog([NSString stringWithFormat:@"label: %@	phoneNum: %@", searchString, phoneNum]);
    			CFStringRef tempRef = (CFStringRef)ABMultiValueCopyValueAtIndex(phoneNums, i);
    			phoneNum = (NSString *)tempRef;
    			
    		}
    	}
    

    I've tried everything I can think of. If I don't use line 9 (casting the value as a CFStringRef before I cast it to an NSString), I get a bad access error on line 10. As it is here, it tells me that tempRef "is not a CFString at this time" after it gets assigned. After phoneNum gets assigned to an NSString, if I use a breakpoint and mouse over it, apparently it's an NSCFArray instead, and I later get an unrecognized selector message when I try to send an NSString method.
  • cjpearlcjpearl Posts: 111Registered Users
    edited January 2009
  • nithinreddynithinreddy Posts: 6New Users
    edited March 2009
    Hi all,

    Can anyone help me in retrieving the anniversary date for a person in the address book?

    i retrieved the birthday using the statement ::
    NSString *date = (NSString *)ABRecordCopyValue(person, kABPersonBirthdayProperty);

    But i was not able to find how to do the same for anniversaries dates.

    Thanks & Regards,
    -NR
  • satishsatish Posts: 3New Users
    edited January 2010
    kABPersonBirthdayProperty returns of type NSDate..so you cannot assign
    NSString *date = (NSString *)ABRecordCopyValue(person, kABPersonBirthdayProperty);

    it has to be
    NSdate *date =(NSDate*)ABRecordCopyValue(person, kABPersonBirthdayProperty);





    --Satish
Sign In or Register to comment.