2

I am not able to retrieve mobile number from below code, i am trying lots of thing but unable to solve it. please help me

//this below code is for saving mobile number and phone number

        ABRecordRef newPerson = ABPersonCreate(); 

        ABMultiValueAddValueAndLabel(phone, (__bridge CFTypeRef)(txtMob2.text), kABHomeLabel, NULL);
        ABRecordSetValue(newPerson, kABPersonPhoneProperty, phone, &error);

//image
        NSData *dataref = UIImagePNGRepresentation(imgProfile.image);
        ABPersonSetImageData(newPerson, (__bridge CFDataRef)(dataref), &error);

        ABAddressBookAddRecord(addressbook, newPerson, &error);
        ABAddressBookSave(addressbook, nil);
        CFRelease(newPerson);

// Now Below code is of displaying details.

 - (void)displayContacts
{
    int i;
    appDelegate.arr_contact = [[NSMutableArray alloc] init];

    ABAddressBookRef contactBook =ABAddressBookCreateWithOptions(nil, NULL);
    NSArray *allData = (__bridge_transfer NSArray *)(ABAddressBookCopyArrayOfAllPeople(contactBook));
    NSUInteger contactNum = 0;

    NSInteger recordId;
    ABRecordRef recId;

    for (i =0; i < [allData count]; i++)
    {
        appDelegate.dict_contact =[[NSMutableDictionary alloc] init];
        ABRecordRef ref =(__bridge ABRecordRef)(allData[i]);

        //mobile no and phone no
        ABMultiValueRef mobNum = ABRecordCopyValue(ref, kABPersonPhoneProperty);
        CFIndex PhoneCount = ABMultiValueGetCount(mobNum);
        for (int k  = 0; k <PhoneCount; k++) {

           strMobile = (__bridge NSString *)(ABMultiValueCopyLabelAtIndex(mobNum, k));
           strPhone = (__bridge NSString *)(ABMultiValueCopyValueAtIndex(mobNum, k));

            if ([strMobile isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
            {
                mobileno = (__bridge NSString*)ABMultiValueCopyValueAtIndex(mobNum, k);
            }
            else if ([strPhone isEqualToString:(NSString *)kABHomeLabel])
            {
                phoneno = (__bridge NSString*)ABMultiValueCopyValueAtIndex(mobNum, k);
                break;
            }

            NSLog(@"Mobile: %@ phone: %@, %@",mobileno, phoneno, strPhone);
            [appDelegate.dict_contact setObject:[NSString stringWithFormat:@"%@",mobileno] forKey:@"mobno"];
            [appDelegate.dict_contact setObject:[NSString stringWithFormat:@"%@",phoneno] forKey:@"phnno"];
        }

        //image
        NSData *imgData = (__bridge NSData *)ABPersonCopyImageDataWithFormat(ref, kABPersonImageFormatThumbnail);
        UIImage *image;
        if (imgData)
        {
           image = [UIImage imageWithData:imgData];
            NSLog(@"add image: %@",image);
        }else
        {
            image = [UIImage imageNamed:@"dummy.png"];
        }
        [appDelegate.dict_contact setObject:image forKey:@"image"];
        [appDelegate.arr_contact addObject:appDelegate.dict_contact];
    }
}

Please check my edited code.

Abha
  • 1,032
  • 1
  • 13
  • 36

2 Answers2

-1

Please See this code....

-(void)GetAddressBook
{
   Contacts = [[NSMutableArray alloc]init];

   if (ABAddressBookCreateWithOptions) {

   @try {

        ABAddressBookRef addressBook = ABAddressBookCreate();
        // NSArray *people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
        if (!addressBook) {
            NSLog(@"opening address book");
        }
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

        NSLog(@"opening address book ==%ld",nPeople);

        for (int i=0;i < nPeople;i++) {

            NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
            ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
            NSString *Contact;
            ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));
            CFStringRef firstName, lastName;
            NSMutableArray *array = [[NSMutableArray alloc]init];
            NSString *email;
            firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
            lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);
            ABMultiValueRef multiValueRef = ABRecordCopyValue(ref, kABPersonEmailProperty);
            array = [(__bridge NSMutableArray *)ABMultiValueCopyArrayOfAllValues(multiValueRef) mutableCopy];
            email = ([array count] > 0) ? array[0] : @"";

            if(firstName)
            {
                Contact = [NSString stringWithFormat:@"%@", firstName];
                if(lastName)
                    Contact = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
            }
            [dOfPerson setObject:Contact forKey:@"name"];
            [dOfPerson setObject:[NSString stringWithFormat:@"%d", i] forKey:@"id"];
            [dOfPerson setObject:[NSString stringWithFormat:@"%@",@""] forKey:@"found"];
            [dOfPerson setObject:email forKey:@"email"];

            NSString* mobileLabel;
            for(CFIndex j = 0; j< ABMultiValueGetCount(phones); j++)
            {
                mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, j);
                if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
                {
                    [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"];
                }
                else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
                {
                    [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, j) forKey:@"Phone"];
                    break ;
                }
            }
            [Contacts addObject:dOfPerson];
        }
    }
    @catch (NSException * e) {
        NSLog(@"Exception: %@", e);


    }
    dispatch_async(dispatch_get_main_queue(), ^{



    });

Here you will get Phone number, First Name, Last name and Email Id. Here Contacts is an NSMutableArray. Also view this answer for a complete set of code.

contact details from ipad using objective-c

Community
  • 1
  • 1
IronManGill
  • 7,222
  • 2
  • 31
  • 52
  • Still as it is.I just get only one value either mobile number or landline number but i inserted both.Also issue of Retrieving an image. :( – Abha Aug 01 '13 at 12:09
  • @Abha This Does not include retrieving an image .... you get one value because i hav include kABPersonPhoneProperty ... u can include others too :) – IronManGill Aug 01 '13 at 12:23
  • 1
    ABAddressBookRef addressBook = ABAddressBookCreate(); was deprecated in ios6.0 if you use this you shouldn't get in iPhone but you'll get in Simulator You have to use this ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error); – Vishnu Aug 01 '13 at 13:36
-1

Try This In this code I'm getting both the name and mobile number if you want the mobile number only use this kABPersonPhoneProperty:

CFErrorRef * error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for (int i=0;i < nPeople;i++)
{
    NSString* name;
    NSMutableDictionary* tempContactDic = [NSMutableDictionary new];
    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
    CFStringRef firstName;
    firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
    name = (__bridge NSString *)(firstName);
    [tempContactDic setValue:name forKey:@"name"];
    //fetch email id
    NSString *strEmail;
    NSMutableString* strMobile;
    ABMultiValueRef email = ABRecordCopyValue(ref, kABPersonPhoneProperty);
    CFStringRef tempEmailref = ABMultiValueCopyValueAtIndex(email, 0);
    strEmail = (__bridge  NSString *)tempEmailref;
    strEmail = [strEmail stringByReplacingOccurrencesOfString:@"-" withString:@""];
    strEmail = [strEmail stringByReplacingOccurrencesOfString:@"(" withString:@""];
    strEmail = [strEmail stringByReplacingOccurrencesOfString:@")" withString:@""];
    strEmail = [strEmail stringByReplacingOccurrencesOfString:@" " withString:@""];
    strEmail = [strEmail stringByReplacingOccurrencesOfString:@"+" withString:@""];
    strMobile = [[NSMutableString alloc] initWithString:strEmail];
    if ([strEmail length] == 10) {
        [strMobile insertString:@"91" atIndex:0];
    }
    else {

    }
    NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
    [f setNumberStyle:NSNumberFormatterDecimalStyle];
    NSNumber * myNumber = [f numberFromString:strMobile];
    [tempContactDic setValue:myNumber forKey:@"phone"];

    [contactsArray addObject:tempContactDic];
}
/// With below two steps of code you'll get the contacts in alphabetical order
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
[contactsArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
Vishnu
  • 354
  • 3
  • 19
  • thanks for code but my issue is i want mobile no. as well as phone number but i did not get. I get only one value either mobile number and landline number. When i inserted numbers, it save perfectly but having issue for displaying it. – Abha Aug 03 '13 at 05:08