How to acess contacts of iPhone in IOS 6

后端 未结 3 1442
一整个雨季
一整个雨季 2021-01-03 16:53

I want to show contacts and details of contacts in my application.List of contacts and after selecting any contact details of that contact will show on next page using addre

相关标签:
3条回答
  • 2021-01-03 17:17

    Add Framwork

    #import <AddressBook/AddressBook.h>
    #import <AddressBook/ABPerson.h>
    #import <AddressBookUI/AddressBookUI.h>
    

    use delegate

    <ABPeoplePickerNavigationControllerDelegate,ABPersonViewControllerDelegate,ABNewPersonViewControllerDelegate,ABUnknownPersonViewControllerDelegate> 
    

    You can show your contact by ABPeoplePickerNavigationController

    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
            [[picker navigationBar] setBarStyle:UIBarStyleBlack];
            picker.peoplePickerDelegate = self;
            // Display only a person's phone, email, and birthdate
            NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],nil];
    
            picker.displayedProperties = displayedItems;
            // Show the picker
            [self presentModalViewController:picker animated:YES];
            [picker release];   
    

    and after you initialize then you need to use following method

    #pragma mark - ABPeopelPickerNavigationController Delegate and DataSource Methods
    
    - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
    {
        [self dismissModalViewControllerAnimated:YES];
    }
    
    - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
    {
       // For get People detail
    }
    
    - (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownCardViewController didResolveToPerson:(ABRecordRef)person
    {
    }
    
    - (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person
    {
    }
    
    - (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
    {
        return YES;
    }
    
    - (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier;
    {
        return YES;
    }
    
    0 讨论(0)
  • 2021-01-03 17:19

    Following code for retrieving contact details.

    - (void)viewDidLoad {
        [super viewDidLoad];
        contactList=[[NSMutableArray alloc] init];
        ABAddressBookRef m_addressbook = ABAddressBookCreate();
    
        if (!m_addressbook) {
            NSLog(@"opening address book");
        }
    
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
        CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
    
        for (int i=0;i &lt; nPeople;i++) { 
            NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];
    
            ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);
    
            //For username and surname
            ABMultiValueRef phones =(NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty);
            CFStringRef firstName, lastName;
            firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
            lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);
            [dOfPerson setObject:[NSString stringWithFormat:@"%@ %@", firstName, lastName] forKey:@"name"];
    
            //For Email ids
            ABMutableMultiValueRef eMail  = ABRecordCopyValue(ref, kABPersonEmailProperty);
            if(ABMultiValueGetCount(eMail) &gt; 0) {
                [dOfPerson setObject:(NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:@"email"];
    
            }
    
            //For Phone number
            NSString* mobileLabel;
            for(CFIndex i = 0; i &lt; ABMultiValueGetCount(phones); i++) {
                mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
                if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
                {
                    [dOfPerson setObject:(NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
                }
                else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
                {
                    [dOfPerson setObject:(NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
                    break ;
                }
    
            [contactList addObject:dOfPerson];
            CFRelease(ref);
            CFRelease(firstName);
            CFRelease(lastName);
        }
        NSLog(@"array is %@",contactList);
        }
    }
    

    You can download sample code and find more details from the tutorial on my site.

    0 讨论(0)
  • 2021-01-03 17:36

    use

    ABRecordID recordId;
    ABAddressBookRef _addressBookRef = ABAddressBookCreate ();
    NSArray* allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(_addressBookRef);
    NSString* compositeName = (NSString *)ABRecordCopyCompositeName((ABRecordRef)record);
    recordId = ABRecordGetRecordID(record);
    ABMultiValueRef emails = ABRecordCopyValue(record, kABPersonEmailProperty);
    

    Do proper release just a snippet

    0 讨论(0)
提交回复
热议问题