【问题标题】:my contact not add in addressbook in ios 7我的联系人没有添加到 iOS 7 的通讯录中
【发布时间】:2013-11-12 13:11:22
【问题描述】:

我是 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];
    }

【问题讨论】:

    标签: iphone ios5 ios6 ios7


    【解决方案1】:

    检查地址簿是否允许

        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);
    

    【讨论】:

    【解决方案2】:

    试试这个代码...

    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】:
     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
    }
    

    【讨论】:

    【解决方案4】:

    修改代码:

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

    如果 (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) { ABAddressBookRequestAccessWithCompletion(addressBookRef,^(授予布尔值,CFErrorRef 错误){ // 第一次访问被授予,添加联系人 [自我添加联系方式]; }); } 否则如果(ABAddressBookGetAuthorizationStatus()== kABAuthorizationStatusAuthorized){ // 用户之前已授予访问权限,添加联系人 [自我添加联系方式]; }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多