Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Get contact name and phone number

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 572
    Comment on it

    Hi all,

    If you want to get contact name and contact number from your iphone contact list then below two methods can help you to get contact name and contact phone number .

    Note: In this you will only get those contacts,which contain phone number or mobile number.

    [self addFromAddressBook];
    

    above code to call method addFromAddressBook.

    -(void)addFromAddressBook{
        ABAddressBookRef addressBook = ABAddressBookCreate();
    
        __block BOOL accessGranted = NO;
    
        if (&ABAddressBookRequestAccessWithCompletion != NULL) { // We are on iOS 6
            dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    
            ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
                accessGranted = granted;
                dispatch_semaphore_signal(semaphore);
            });
    
            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        }
    
        else { // We are on iOS 5 or Older
            accessGranted = YES;
            [self getContactsWithAddressBook:addressBook];
        }
    
        if (accessGranted) {
            [self getContactsWithAddressBook:addressBook];
        }
    
    }
    
    
    // Get the contacts.
    - (void)getContactsWithAddressBook:(ABAddressBookRef )addressBook {
    
        NSMutableArray *contactList = [[NSMutableArray alloc] init];
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
    
        for (int i=0;i < nPeople;i++) {
            NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
    
            ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
    
            //For username and surname
            ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));
    
            CFStringRef firstName, lastName;
            firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
            lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);
            if (phones) {
                NSString *mobile;
                for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++) {
                    mobile = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, j);
                    if ([mobile isEqualToString:(NSString *)kABPersonPhoneMobileLabel]||[mobile isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel]) {
                        [dOfPerson setObject:[NSString stringWithFormat:@"%@ %@", firstName, lastName] forKey:@"name"];
                    }
                }
    
            }
    
            //For Phone number
            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 ;
                }
    
            }
            if(![[dOfPerson allKeys] isEqualToArray:[NSMutableArray new]])
                [contactList addObject:dOfPerson];
    
        }
       // add your code here to use contact list 
            NSLog(@"Contacts = %@",contactList);
        }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: