如何询问用户访问的联系*限然后建立和打开联系人?

时间:2023-01-17 14:11:49

Ok, so I got a contact to go to my phone and it works great except for two things. First, It fails the first time because it asks me for access to the contacts. How can I make it so it adds it after the user gives access to contacts? Second, Is there a way to open up the contact so the user can view it after it is made? This is what I did :

好的,所以我有一个联系人去我的手机,除了两件事之外它很有用。首先,它第一次失败,因为它要求我访问联系人。如何在用户访问联系人后添加它?第二,有没有办法打开联系人,以便用户在制作后可以查看它?这就是我做的:

- (IBAction)addContact:(id)sender {
    ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init];
    ABAddressBookRef addressBook = [peoplePicker addressBook];

    // create person record

    ABRecordRef person = ABPersonCreate();
    // set name and other string values

    NSString *organization = @"American Business Center";
    NSString *personEmail = @"leasing@americanbusinesscenter.com";
    NSString *phoneNo = @"(727)536-6379";
    NSString *webURL = @"www.AmericanBusinessCenter.com";
    NSString *addressOne = @"8340 Ulmerton Road";
    NSString *addressTwo = @"Suite 202";
    NSString *cityName =@ "Largo";
    NSString *stateName = @"Florida";
    NSString *pinCode = @"34209";
    NSString *country = @"United States";

    CFErrorRef cfError=nil;


    ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFStringRef)organization, NULL);

    if (webURL)
    {
        ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef) webURL, kABPersonHomePageLabel, NULL);
        ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, nil);
        CFRelease(urlMultiValue);
    }

    if (personEmail)
    {
        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) personEmail, kABWorkLabel, NULL);
        ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
        CFRelease(emailMultiValue);
    }

    if (phoneNo)
    {
        ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        NSArray *venuePhoneNumbers = [phoneNo componentsSeparatedByString:@" or "];
        for (NSString *venuePhoneNumberString in venuePhoneNumbers)
            ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) venuePhoneNumberString, kABPersonPhoneMainLabel, NULL);
        ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
        CFRelease(phoneNumberMultiValue);
    }

    // add address

    ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];

    if (addressOne)
    {
        if (addressTwo)
            addressDictionary[(NSString *) kABPersonAddressStreetKey] = [NSString stringWithFormat:@"%@\n%@", addressOne, addressTwo];
        else
            addressDictionary[(NSString *) kABPersonAddressStreetKey] = addressOne;
    }

    if (cityName)
        addressDictionary[(NSString *)kABPersonAddressCityKey] = cityName;
    if (stateName)
        addressDictionary[(NSString *)kABPersonAddressStateKey] = stateName;
    if (pinCode)
        addressDictionary[(NSString *)kABPersonAddressZIPKey] = pinCode;
    if (country)
        addressDictionary[(NSString *)kABPersonAddressCountryKey] = country;

    ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFDictionaryRef) addressDictionary, kABWorkLabel, NULL);
    ABRecordSetValue(person, kABPersonAddressProperty, multiAddress, NULL);
    CFRelease(multiAddress);


    //Add person Object to addressbook Object.
    ABAddressBookAddRecord(addressBook, person, &cfError);

    if (ABAddressBookSave(addressBook, nil)) {
        NSLog(@"\nPerson Saved successfuly");
    } else {
        NSLog(@"\n Error Saving person to AddressBook");
    }
}

Edit

Based on LML's answer I have done this :

根据LML的答案我做了这个:

- (IBAction)addContact:(id)sender {
    ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init];
    ABAddressBookRef addressBook = [peoplePicker addressBook];

    // create person record

    ABRecordRef person = ABPersonCreate();
    // set name and other string values

    NSString *organization = @"American Business Center";
    NSString *personEmail = @"leasing@americanbusinesscenter.com";
    NSString *phoneNo = @"(727)536-6379";
    NSString *webURL = @"www.AmericanBusinessCenter.com";
    NSString *addressOne = @"8340 Ulmerton Road";
    NSString *addressTwo = @"Suite 202";
    NSString *cityName =@ "Largo";
    NSString *stateName = @"Florida";
    NSString *pinCode = @"34209";
    NSString *country = @"United States";

    CFErrorRef cfError=nil;


    ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFStringRef)organization, NULL);

    if (webURL)
    {
        ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef) webURL, kABPersonHomePageLabel, NULL);
        ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, nil);
        CFRelease(urlMultiValue);
    }

    if (personEmail)
    {
        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) personEmail, kABWorkLabel, NULL);
        ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
        CFRelease(emailMultiValue);
    }

    if (phoneNo)
    {
        ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        NSArray *venuePhoneNumbers = [phoneNo componentsSeparatedByString:@" or "];
        for (NSString *venuePhoneNumberString in venuePhoneNumbers)
            ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) venuePhoneNumberString, kABPersonPhoneMainLabel, NULL);
        ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
        CFRelease(phoneNumberMultiValue);
    }

    // add address

    ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];

    if (addressOne)
    {
        if (addressTwo)
            addressDictionary[(NSString *) kABPersonAddressStreetKey] = [NSString stringWithFormat:@"%@\n%@", addressOne, addressTwo];
        else
            addressDictionary[(NSString *) kABPersonAddressStreetKey] = addressOne;
    }

    if (cityName)
        addressDictionary[(NSString *)kABPersonAddressCityKey] = cityName;
    if (stateName)
        addressDictionary[(NSString *)kABPersonAddressStateKey] = stateName;
    if (pinCode)
        addressDictionary[(NSString *)kABPersonAddressZIPKey] = pinCode;
    if (country)
        addressDictionary[(NSString *)kABPersonAddressCountryKey] = country;

    ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFDictionaryRef) addressDictionary, kABWorkLabel, NULL);
    ABRecordSetValue(person, kABPersonAddressProperty, multiAddress, NULL);
    CFRelease(multiAddress);

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            // First time access has been granted, add the contact
            if(error == NULL){
                NSLog(@"Success");
                ABAddressBookAddRecord(addressBook, person, &cfError);
            }
        });
        CFRelease(addressBook);
        CFRelease(person);
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        // The user has previously given access, add the contact
        ABAddressBookSave(addressBook, &cfError);
        if(cfError == NULL){
            NSLog(@"Success") ;
        }
        CFRelease(addressBook);
        CFRelease(person);
    }
    else {
        NSLog(@"Adding to address book failed");
    }
}

Now, it crashes when I press the button and I am getting the error :

现在,当我按下按钮时它会崩溃并且我收到错误:

Passing 'const CFErrorRef *' (aka 'struct __CFError *const *') to parameter of type 'CFErrorRef *' (aka 'struct __CFError **') discards qualifiers

将'const CFErrorRef *'(又名'struct __CFError * const *')传递给'CFErrorRef *'类型的参数(又名'struct __CFError **')会丢弃限定符

I think I misinterpreted :P

我想我误解了:P

2 个解决方案

#1


3  

Figured it out!

弄清楚了!

To ask permission to access contact, then make and display use the following:

要获得访问联系人的权限,请制作并显示以下内容:


To ask permission or notify user to allow contacts access :

- (IBAction)addContact:(id)sender {
    // Request authorization to Address Book
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            if (granted) {
                [self addContactToAddressBook];
            } else {
                [self customAlert];
            }
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        [self addContactToAddressBook];
    }
    else {
        [self customAlert];
    }
}

To make contact after permission is accepted :


- (void)addContactToAddressBook {
    CFErrorRef error = NULL;
    ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init];
    ABAddressBookRef addressBook = [peoplePicker addressBook];

    // create person record

    ABRecordRef person = ABPersonCreate();
    // set name and other string values

    NSString *organization = @"American Business Center";
    NSString *personEmail = @"leasing@americanbusinesscenter.com";
    NSString *phoneNo = @"(727)536-6379";
    NSString *webURL = @"www.AmericanBusinessCenter.com";
    NSString *addressOne = @"8340 Ulmerton Road";
    NSString *addressTwo = @"Suite 202";
    NSString *cityName =@ "Largo";
    NSString *stateName = @"Florida";
    NSString *pinCode = @"34209";
    NSString *country = @"United States";

    ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFStringRef)organization, NULL);

    if (webURL)
    {
        ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef) webURL, kABPersonHomePageLabel, NULL);
        ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, nil);
        CFRelease(urlMultiValue);
    }

    if (personEmail)
    {
        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) personEmail, kABWorkLabel, NULL);
        ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
        CFRelease(emailMultiValue);
    }

    if (phoneNo)
    {
        ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        NSArray *venuePhoneNumbers = [phoneNo componentsSeparatedByString:@" or "];
        for (NSString *venuePhoneNumberString in venuePhoneNumbers)
            ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) venuePhoneNumberString, kABPersonPhoneMainLabel, NULL);
        ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
        CFRelease(phoneNumberMultiValue);
    }

    // add address

    ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];

    if (addressOne)
    {
        if (addressTwo)
            addressDictionary[(NSString *) kABPersonAddressStreetKey] = [NSString stringWithFormat:@"%@\n%@", addressOne, addressTwo];
        else
            addressDictionary[(NSString *) kABPersonAddressStreetKey] = addressOne;
    }

    if (cityName)
        addressDictionary[(NSString *)kABPersonAddressCityKey] = cityName;
    if (stateName)
        addressDictionary[(NSString *)kABPersonAddressStateKey] = stateName;
    if (pinCode)
        addressDictionary[(NSString *)kABPersonAddressZIPKey] = pinCode;
    if (country)
        addressDictionary[(NSString *)kABPersonAddressCountryKey] = country;

    ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFDictionaryRef) addressDictionary, kABWorkLabel, NULL);
    ABRecordSetValue(person, kABPersonAddressProperty, multiAddress, NULL);

    ABAddressBookAddRecord(addressBook, person, &error);
    ABAddressBookSave(addressBook, &error);

Then open new contact

    ABPersonViewController *ctrl = [[ABPersonViewController alloc]init];

    ctrl.allowsEditing = NO;
    [ctrl setPersonViewDelegate:self];
    [ctrl setDisplayedPerson:person];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    UILabel * titleView = [[UILabel alloc] initWithFrame:CGRectZero];
    titleView.backgroundColor = [UIColor clearColor];
    titleView.font = [UIFont boldSystemFontOfSize:18.0];
    titleView.text = @"Info";
    titleView.textColor = [UIColor whiteColor];
    ctrl.navigationItem.titleView = titleView;
    [titleView sizeToFit];
    [self.navigationController pushViewController:ctrl animated:YES];
}

#2


2  

   - (IBAction)addContact:(id)sender {
    ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init];
    ABAddressBookRef addressBook = [peoplePicker addressBook];

    // create person record

    ABRecordRef person = ABPersonCreate();
    // set name and other string values

    NSString *organization = @"American Business Center";
    NSString *personEmail = @"leasing@americanbusinesscenter.com";
    NSString *phoneNo = @"(727)536-6379";
    NSString *webURL = @"www.AmericanBusinessCenter.com";
    NSString *addressOne = @"8340 Ulmerton Road";
    NSString *addressTwo = @"Suite 202";
    NSString *cityName =@ "Largo";
    NSString *stateName = @"Florida";
    NSString *pinCode = @"34209";
    NSString *country = @"United States";

    CFErrorRef error=nil;


    ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFStringRef)organization, NULL);

    if (webURL)
    {
        ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef) webURL, kABPersonHomePageLabel, NULL);
        ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, nil);
        CFRelease(urlMultiValue);
    }

    if (personEmail)
    {
        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) personEmail, kABWorkLabel, NULL);
        ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
        CFRelease(emailMultiValue);
    }

    if (phoneNo)
    {
        ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        NSArray *venuePhoneNumbers = [phoneNo componentsSeparatedByString:@" or "];
        for (NSString *venuePhoneNumberString in venuePhoneNumbers)
            ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) venuePhoneNumberString, kABPersonPhoneMainLabel, NULL);
        ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
        CFRelease(phoneNumberMultiValue);
    }

    // add address

    ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];

    if (addressOne)
    {
        if (addressTwo)
            addressDictionary[(NSString *) kABPersonAddressStreetKey] = [NSString stringWithFormat:@"%@\n%@", addressOne, addressTwo];
        else
            addressDictionary[(NSString *) kABPersonAddressStreetKey] = addressOne;
    }

    if (cityName)
        addressDictionary[(NSString *)kABPersonAddressCityKey] = cityName;
    if (stateName)
        addressDictionary[(NSString *)kABPersonAddressStateKey] = stateName;
    if (pinCode)
        addressDictionary[(NSString *)kABPersonAddressZIPKey] = pinCode;
    if (country)
        addressDictionary[(NSString *)kABPersonAddressCountryKey] = country;

    ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFDictionaryRef) addressDictionary, kABWorkLabel, NULL);
    ABRecordSetValue(person, kABPersonAddressProperty, multiAddress, NULL);
    CFRelease(multiAddress);


    //Add person Object to addressbook Object.
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            // First time access has been granted, add the contact
            if(error == NULL){
                NSLog(@"Success");
            }
        });
        CFRelease(addressBook);
        CFRelease(person);
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        // The user has previously given access, add the contact
        ABAddressBookSave(addressBook, &error);
        if(error == NULL){
            NSLog(@"Success") ;
        }
        CFRelease(addressBook);
        CFRelease(person);
    }
    else {
        NSLog(@"Adding to address book failed");
    }
}

#1


3  

Figured it out!

弄清楚了!

To ask permission to access contact, then make and display use the following:

要获得访问联系人的权限,请制作并显示以下内容:


To ask permission or notify user to allow contacts access :

- (IBAction)addContact:(id)sender {
    // Request authorization to Address Book
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            if (granted) {
                [self addContactToAddressBook];
            } else {
                [self customAlert];
            }
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        [self addContactToAddressBook];
    }
    else {
        [self customAlert];
    }
}

To make contact after permission is accepted :


- (void)addContactToAddressBook {
    CFErrorRef error = NULL;
    ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init];
    ABAddressBookRef addressBook = [peoplePicker addressBook];

    // create person record

    ABRecordRef person = ABPersonCreate();
    // set name and other string values

    NSString *organization = @"American Business Center";
    NSString *personEmail = @"leasing@americanbusinesscenter.com";
    NSString *phoneNo = @"(727)536-6379";
    NSString *webURL = @"www.AmericanBusinessCenter.com";
    NSString *addressOne = @"8340 Ulmerton Road";
    NSString *addressTwo = @"Suite 202";
    NSString *cityName =@ "Largo";
    NSString *stateName = @"Florida";
    NSString *pinCode = @"34209";
    NSString *country = @"United States";

    ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFStringRef)organization, NULL);

    if (webURL)
    {
        ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef) webURL, kABPersonHomePageLabel, NULL);
        ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, nil);
        CFRelease(urlMultiValue);
    }

    if (personEmail)
    {
        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) personEmail, kABWorkLabel, NULL);
        ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
        CFRelease(emailMultiValue);
    }

    if (phoneNo)
    {
        ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        NSArray *venuePhoneNumbers = [phoneNo componentsSeparatedByString:@" or "];
        for (NSString *venuePhoneNumberString in venuePhoneNumbers)
            ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) venuePhoneNumberString, kABPersonPhoneMainLabel, NULL);
        ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
        CFRelease(phoneNumberMultiValue);
    }

    // add address

    ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];

    if (addressOne)
    {
        if (addressTwo)
            addressDictionary[(NSString *) kABPersonAddressStreetKey] = [NSString stringWithFormat:@"%@\n%@", addressOne, addressTwo];
        else
            addressDictionary[(NSString *) kABPersonAddressStreetKey] = addressOne;
    }

    if (cityName)
        addressDictionary[(NSString *)kABPersonAddressCityKey] = cityName;
    if (stateName)
        addressDictionary[(NSString *)kABPersonAddressStateKey] = stateName;
    if (pinCode)
        addressDictionary[(NSString *)kABPersonAddressZIPKey] = pinCode;
    if (country)
        addressDictionary[(NSString *)kABPersonAddressCountryKey] = country;

    ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFDictionaryRef) addressDictionary, kABWorkLabel, NULL);
    ABRecordSetValue(person, kABPersonAddressProperty, multiAddress, NULL);

    ABAddressBookAddRecord(addressBook, person, &error);
    ABAddressBookSave(addressBook, &error);

Then open new contact

    ABPersonViewController *ctrl = [[ABPersonViewController alloc]init];

    ctrl.allowsEditing = NO;
    [ctrl setPersonViewDelegate:self];
    [ctrl setDisplayedPerson:person];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    UILabel * titleView = [[UILabel alloc] initWithFrame:CGRectZero];
    titleView.backgroundColor = [UIColor clearColor];
    titleView.font = [UIFont boldSystemFontOfSize:18.0];
    titleView.text = @"Info";
    titleView.textColor = [UIColor whiteColor];
    ctrl.navigationItem.titleView = titleView;
    [titleView sizeToFit];
    [self.navigationController pushViewController:ctrl animated:YES];
}

#2


2  

   - (IBAction)addContact:(id)sender {
    ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init];
    ABAddressBookRef addressBook = [peoplePicker addressBook];

    // create person record

    ABRecordRef person = ABPersonCreate();
    // set name and other string values

    NSString *organization = @"American Business Center";
    NSString *personEmail = @"leasing@americanbusinesscenter.com";
    NSString *phoneNo = @"(727)536-6379";
    NSString *webURL = @"www.AmericanBusinessCenter.com";
    NSString *addressOne = @"8340 Ulmerton Road";
    NSString *addressTwo = @"Suite 202";
    NSString *cityName =@ "Largo";
    NSString *stateName = @"Florida";
    NSString *pinCode = @"34209";
    NSString *country = @"United States";

    CFErrorRef error=nil;


    ABRecordSetValue(person, kABPersonOrganizationProperty, (__bridge CFStringRef)organization, NULL);

    if (webURL)
    {
        ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef) webURL, kABPersonHomePageLabel, NULL);
        ABRecordSetValue(person, kABPersonURLProperty, urlMultiValue, nil);
        CFRelease(urlMultiValue);
    }

    if (personEmail)
    {
        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) personEmail, kABWorkLabel, NULL);
        ABRecordSetValue(person, kABPersonEmailProperty, emailMultiValue, nil);
        CFRelease(emailMultiValue);
    }

    if (phoneNo)
    {
        ABMutableMultiValueRef phoneNumberMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        NSArray *venuePhoneNumbers = [phoneNo componentsSeparatedByString:@" or "];
        for (NSString *venuePhoneNumberString in venuePhoneNumbers)
            ABMultiValueAddValueAndLabel(phoneNumberMultiValue, (__bridge CFStringRef) venuePhoneNumberString, kABPersonPhoneMainLabel, NULL);
        ABRecordSetValue(person, kABPersonPhoneProperty, phoneNumberMultiValue, nil);
        CFRelease(phoneNumberMultiValue);
    }

    // add address

    ABMutableMultiValueRef multiAddress = ABMultiValueCreateMutable(kABMultiDictionaryPropertyType);
    NSMutableDictionary *addressDictionary = [[NSMutableDictionary alloc] init];

    if (addressOne)
    {
        if (addressTwo)
            addressDictionary[(NSString *) kABPersonAddressStreetKey] = [NSString stringWithFormat:@"%@\n%@", addressOne, addressTwo];
        else
            addressDictionary[(NSString *) kABPersonAddressStreetKey] = addressOne;
    }

    if (cityName)
        addressDictionary[(NSString *)kABPersonAddressCityKey] = cityName;
    if (stateName)
        addressDictionary[(NSString *)kABPersonAddressStateKey] = stateName;
    if (pinCode)
        addressDictionary[(NSString *)kABPersonAddressZIPKey] = pinCode;
    if (country)
        addressDictionary[(NSString *)kABPersonAddressCountryKey] = country;

    ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFDictionaryRef) addressDictionary, kABWorkLabel, NULL);
    ABRecordSetValue(person, kABPersonAddressProperty, multiAddress, NULL);
    CFRelease(multiAddress);


    //Add person Object to addressbook Object.
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            // First time access has been granted, add the contact
            if(error == NULL){
                NSLog(@"Success");
            }
        });
        CFRelease(addressBook);
        CFRelease(person);
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
        // The user has previously given access, add the contact
        ABAddressBookSave(addressBook, &error);
        if(error == NULL){
            NSLog(@"Success") ;
        }
        CFRelease(addressBook);
        CFRelease(person);
    }
    else {
        NSLog(@"Adding to address book failed");
    }
}