2

我是iOS开发的初学者;此代码在 iOS 6 中有效,但在 iOS 7 中无效……我的代码如下,我想将联系人添加到我的通讯录中。我正在开发一个应用程序,我已经通过了许多链接,并且我有以下代码,但现在我被卡住了......

I have imported:

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

ViewController.m文件

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

4 回答 4

8

检查是否允许通讯录

    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
}

也替换第一行

    CFErrorRef * error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
于 2013-11-01T10:45:52.717 回答
4

试试这个代码...

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");
    }
于 2013-11-01T10:57:09.667 回答
0

修改后的代码:

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

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) { ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool grant, CFErrorRef error) { // 首次访问已被授予,添加联系人 [self addContact]; }); } else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) { // 用户之前已授予访问权限,添加联系人 [self addContact]; }

于 2015-01-24T08:31:35.927 回答
-2
 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
}
于 2013-11-05T07:24:56.767 回答