my contact not add in addressbook in ios 7

2019-05-20 21:18发布

I am a beginner in iOS development; this code is working in iOS 6 but does not work in iOS 7 ... My code is below, I want to add contacts into my address book. I am developing an app where I have gone through many links, and I have following code, but now I am stuck...

I have imported:

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

ViewController.m file

ABAddressBookRef ab = ABAddressBookCreate();
    // To add a new ab entry with telephone number
    ABRecordRef newPerson = ABPersonCreate();

    ABRecordSetValue(newPerson, kABPersonFirstNameProperty, (__bridge CFStringRef) nameFirststr, nil);

    ABRecordSetValue(newPerson, kABPersonLastNameProperty, (__bridge CFStringRef)@"Jones", nil);

    //phone
    ABMutableMultiValueRef phoneNumberMultiValue =  ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(phoneNumberMultiValue ,(__bridge CFStringRef)myphone,kABPersonPhoneMobileLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonPhoneProperty,  phoneNumberMultiValue, nil);

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

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

        //    if (true)
        //        addressDictionary[(NSString *)kABPersonAddressCityKey] = @"city";
        //    if (true)
        //        addressDictionary[(NSString *)kABPersonAddressStateKey] = @"city";
        //    if (true)
        //        addressDictionary[(NSString *)kABPersonAddressZIPKey] = @"city";
        //    if (true)
        //        addressDictionary[(NSString *)kABPersonAddressCountryKey] = @"city";
    }


    ABMultiValueAddValueAndLabel(multiAddress, (__bridge CFDictionaryRef) addressDictionary, kABWorkLabel, NULL);

    ABRecordSetValue(newPerson, kABPersonAddressProperty, multiAddress, nil);

    // email

    if (emailstr)
    {
        ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef) emailstr, kABWorkLabel, NULL);

        ABRecordSetValue(newPerson, kABPersonEmailProperty, emailMultiValue, nil);
    }
    if (Organization)
    {
        ABRecordSetValue(newPerson, kABPersonOrganizationProperty, (__bridge CFStringRef)Organization, nil);

    }
    if (title)
    {
        ABRecordSetValue(newPerson, kABPersonJobTitleProperty, (__bridge CFStringRef)title, nil);

    }
    if (notes)
    {
        ABRecordSetValue(newPerson, kABPersonNoteProperty, (__bridge CFStringRef)notes, nil);
    }


    if (webUrl)
    {

        ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
        ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef) webUrl, kABPersonHomePageLabel, NULL);
        ABRecordSetValue(newPerson, kABPersonURLProperty, urlMultiValue, nil);
    }

    ABAddressBookAddRecord(ab, newPerson, nil);
    ABAddressBookSave(ab,NULL);
    if (ABAddressBookSave(ab, nil)) {
        NSLog(@"\nPerson Saved successfuly");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Your contact sucessfully Add" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alert show];
    } else {
        NSLog(@"\n Error Saving person to AddressBook");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Error...!!!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alert show];
    }

4条回答
可以哭但决不认输i
2楼-- · 2019-05-20 22:07

try this code...

ABPeoplePickerNavigationController *peoplePicker=[[ABPeoplePickerNavigationController alloc] init];
    ABAddressBookRef addressBook = [peoplePicker addressBook];

    // create person record

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

    UIImage *personImage = [UIImage imageNamed:@"cinema.png"];
    NSData *dataRef = UIImagePNGRepresentation(personImage);

    NSString *firstName=@"Raj";
    NSString *lastName=@"Patel";

    NSString *organization=@"Ilesh  Pvt Ltd.";
    NSString *jobTitle=@"iPhone App Developer";
    NSString *departMent=@"Mobile Division";
    NSString *webURL=@"http://www.google.com";
    NSString *personEmail=@"goel.anjan@gmail.com";
    NSString *phoneNo=@"913654985 or 76876879 or 845676764";
    NSString *personNote=@"I am just a kid";

    NSString *addressOne=@"Ahmedabad";
    NSString *addressTwo=@"Ahmedabad";

    NSString *cityName=@"Ahmedabad";
    NSString *stateName=@"Gujarat";
    NSString *pinCode=@"38008";
    NSString *country=@"India";

    CFErrorRef cfError=nil;


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

    if (firstName) {
        ABRecordSetValue(person, kABPersonFirstNameProperty, (__bridge CFTypeRef)(firstName) , nil);
    }

    if (lastName) {
        ABRecordSetValue(person, kABPersonLastNameProperty, (__bridge CFTypeRef)(lastName) , nil);
    }

    if (jobTitle) {
        ABRecordSetValue(person, kABPersonJobTitleProperty,(__bridge CFTypeRef)(jobTitle), nil);
    }

    if (departMent) {
        ABRecordSetValue(person, kABPersonDepartmentProperty,(__bridge CFTypeRef)(departMent), nil);
    }

    if (personNote) {
        ABRecordSetValue(person, kABPersonNoteProperty, (__bridge CFTypeRef)(personNote), nil);
    }

    if (dataRef) {
        ABPersonSetImageData(person, (__bridge CFDataRef)dataRef,&cfError);
    }


    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");
    }
查看更多
祖国的老花朵
3楼-- · 2019-05-20 22:09

modified code:

CFErrorRef * error = NULL; ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) { ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) { // First time access has been granted, add the contact [self addContact]; }); } else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) { // The user has previously given access, add the contact [self addContact]; }

查看更多
地球回转人心会变
4楼-- · 2019-05-20 22:12
 ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
        // First time access has been granted, add the contact
        [self addContact];
    });
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
    // The user has previously given access, add the contact
    [self addContact];
}
else
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Access Denied" message:@"Please give access to AddressBook via settings." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    // The user has previously denied access
    // Send an alert telling user to change privacy setting in settings app
}
查看更多
爷、活的狠高调
5楼-- · 2019-05-20 22:14

check if addressbook is allowed

    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
        // First time access has been granted, add the contact
        [self addContact];
    });
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
    // The user has previously given access, add the contact
    [self addContact];
}
else
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Access Denied" message:@"Please give access to AddressBook via settings." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    // The user has previously denied access
    // Send an alert telling user to change privacy setting in settings app
}

also replace first line

    CFErrorRef * error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
查看更多
登录 后发表回答