Adding address book in contact page

有些话、适合烂在心里 提交于 2020-01-15 04:42:35

问题


I want to add an address book in my contact page and I want to do that programmatically i.e without using nib files. Can anyone suggest me a nice tutorial or sample code for that. i have used the codes of the answer given by iPatel and when i am running it is throwing exception and app is getting terminated. thanks and regards.

Here is the edited code.

#import "ContactInfoViewController.h"

@interface ContactInfoViewController ()

@end

@implementation ContactInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor yellowColor];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(gotohomepage:)]autorelease];
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
[[picker navigationBar] setBarStyle:UIBarStyleBlack];
picker.peoplePickerDelegate = self;
NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],nil];


picker.displayedProperties = displayedItems;
[self presentModalViewController:picker animated:YES];
[picker release];


}
- (IBAction)gotohomepage:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{

ABAddressBookRef addressBook = ABAddressBookCreate();
int i;
NSString *strName = @"";
NSString* company = @"";
NSString *address = @"";
NSString *suburb = @"";
NSString *postalcode = @"";
NSString *state = @"";
NSString *country = @"";
NSString *mobile = @"";
NSString *phone = @"";
NSString *emailid = @"";
strName = (NSString *)ABRecordCopyCompositeName((ABRecordRef) person);
CFStringRef name = ABRecordCopyCompositeName((ABRecordRef) person);
company  = (NSString *)ABRecordCopyValue((ABRecordRef) person, kABPersonOrganizationProperty);
NSArray*  allPeople = (NSArray *)ABAddressBookCopyPeopleWithName(addressBook,name);
CFRelease(name);

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*)ABMultiValueCopyLabelAtIndex(multiValue, i);
        if([HomeLabel isEqualToString:@"_$!<Home>!$_"])
        {
            CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(multiValue, i);
            address = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressStreetKey)];
            suburb = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressCityKey)];
            postalcode = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressZIPKey)];
            state = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressStateKey)];
            country = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict,  kABPersonAddressCountryKey)];
            CFRelease(dict);
        }
        CFRelease(HomeLabel);
    }
    CFRelease(multiValue);
}
CFRelease(allPeople);


ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString* mobileLabel = nil;
for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++)
{
    mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
    if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
    {
        mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
        NSLog(@"phone   %@",mobile);
    }
    else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
    {
        phone = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
        NSLog(@"phone   %@",phone);

        CFRelease(mobileLabel);
        break ;
    }
    CFRelease(mobileLabel);

}
CFStringRef value, label;
ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty);
CFIndex count = ABMultiValueGetCount(multi);
if (count == 1)
{
    value = ABMultiValueCopyValueAtIndex(multi, 0);
    emailid = (NSString*) value;
    NSLog(@"self.emailID   %@",emailid);
    CFRelease(value);
}
else
{
    for (CFIndex i = 0; i < count; i++)
    {
        label = ABMultiValueCopyLabelAtIndex(multi, i);
        value = ABMultiValueCopyValueAtIndex(multi, i);
        if (CFStringCompare(label, kABWorkLabel, 0) == 0)
        {
            emailid = (NSString*) value;
            NSLog(@"self.emailID   %@",emailid);
        }
        else if(CFStringCompare(label, kABHomeLabel, 0) == 0)
        {
            emailid = (NSString*) value;
            NSLog(@"self.emailID   %@",emailid);
        }
        CFRelease(label);
        CFRelease(value);
    }
}
CFRelease(multi);
 CFRelease(phones);
CFRelease(addressBook);
[self dismissModalViewControllerAnimated:YES];
return NO;
}
#pragma mark - ABPeopelPickerNavigationController Delegate and DataSource Methods
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
[self dismissModalViewControllerAnimated:YES];
}
- (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;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end

回答1:


EDIT :

First Add all Delegate and Datasource method in your class .h file

<ABPeoplePickerNavigationControllerDelegate,ABPersonViewControllerDelegate,ABNewPersonViewControllerDelegate,ABUnknownPersonViewControllerDelegate>

Create 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];

Add Following Delegate and DataSource method of ABPeopelPickerNavigationController

#pragma mark - ABPeopelPickerNavigationController Delegate and DataSource Methods

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [self dismissModalViewControllerAnimated:YES];
}


- (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;
}

please try below code for get all the information of people from phonebook

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    ABAddressBookRef addressBook = ABAddressBookCreate();

    int i;
    NSString *strName = @"";
    NSString* company = @"";
    NSString *address = @"";
    NSString *suburb = @"";
    NSString *postalcode = @"";
    NSString *state = @"";
    NSString *country = @"";
    NSString *mobile = @"";
    NSString *phone = @"";
    NSString *emailid = @"";


    strName = (NSString *)ABRecordCopyCompositeName((ABRecordRef) person);
    CFStringRef name = ABRecordCopyCompositeName((ABRecordRef) person);
    company  = (NSString *)ABRecordCopyValue((ABRecordRef) person, kABPersonOrganizationProperty);

    NSArray*  allPeople = (NSArray *)ABAddressBookCopyPeopleWithName(addressBook,name);
    CFRelease(name);

    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*)ABMultiValueCopyLabelAtIndex(multiValue, i);
            if([HomeLabel isEqualToString:@"_$!<Home>!$_"])
            {
                CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(multiValue, i);
                address = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressStreetKey)];
                suburb = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressCityKey)];
                postalcode = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressZIPKey)];
                state = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressStateKey)];
                country = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressCountryKey)];

                CFRelease(dict);
            }
            CFRelease(HomeLabel);
        }
        CFRelease(multiValue);
    }
    CFRelease(allPeople);


    ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString* mobileLabel = nil;
    for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++)
    {
        mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
        if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
        {
            mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
            NSLog(@"phone   %@",mobile);
        }
        else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
        {
            phone = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
            NSLog(@"phone   %@",phone);

            CFRelease(mobileLabel);
            break ;
        }
        CFRelease(mobileLabel);

    }
    CFStringRef value, label;
    ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty);
    CFIndex count = ABMultiValueGetCount(multi);
    if (count == 1)
    {
        value = ABMultiValueCopyValueAtIndex(multi, 0);
        emailid = (NSString*) value;
        NSLog(@"self.emailID   %@",emailid);
        CFRelease(value);
    }
    else
    {
        for (CFIndex i = 0; i < count; i++)
        {
            label = ABMultiValueCopyLabelAtIndex(multi, i);
            value = ABMultiValueCopyValueAtIndex(multi, i);

            // check for Work e-mail label
            if (CFStringCompare(label, kABWorkLabel, 0) == 0)
            {
                emailid = (NSString*) value;
                NSLog(@"self.emailID   %@",emailid);
            }
            else if(CFStringCompare(label, kABHomeLabel, 0) == 0)
            {
                emailid = (NSString*) value;
                NSLog(@"self.emailID   %@",emailid);
            }

            CFRelease(label);
            CFRelease(value);
        }
    }
    CFRelease(multi);

        }


    CFRelease(phones);
    CFRelease(addressBook);
    [self dismissModalViewControllerAnimated:YES];

    return NO;

}

For more information read this and this tutorial.

Thanks :)



来源:https://stackoverflow.com/questions/14729959/adding-address-book-in-contact-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!