首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用地址簿框架iOS 9导出联系人时出错?

使用地址簿框架iOS 9导出联系人时出错?
EN

Stack Overflow用户
提问于 2016-04-01 05:38:08
回答 1查看 141关注 0票数 2

通讯录中没有添加联系人。在AddressBook 9中将人保存到iOS中,但在iOS 6中工作。当我添加联系人时,if块将与日志错误保存人一起执行到AddressBook中。

代码语言:javascript
复制
-(void)addContactToPhoneBook{

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

// create person record
person = ABPersonCreate();

cfError = nil;


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

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

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

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))
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Success" message:@"Contact added sucessfully" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert show];
    NSLog(@"\nPerson Saved successfuly");
} else
{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Failed" message:@"Failed to add contact" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
    [alert show];
    NSLog(@"\n Error Saving person to AddressBook");
}  
 }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-01 05:41:21

尝尝这个

代码语言:javascript
复制
- (void)viewDidLoad {
    [super viewDidLoad];

    arrContactName = [[NSMutableArray alloc]init];
    arrPhoneNumber = [[NSMutableArray alloc]init];
    arrAddContact = [[NSMutableArray alloc]init];

}
-(void)viewWillAppear:(BOOL)animated
{
    ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
    {
        ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
            if (granted) {
                [self dismissViewControllerAnimated:YES completion:nil];
            } else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
    {
        [self getAllContacts];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Unable to Access!" message:@"Grant us access now! Change privacy setting in settings app." delegate:self cancelButtonTitle:@"Not now" otherButtonTitles:@"OK", nil];
        [alert show];
    }
}
#pragma mark -
#pragma mark - Get All Contacts
-(void)getAllContacts
{
    CFErrorRef error = NULL;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);

    if (addressBook != nil)
    {
        NSArray *allContacts = (__bridge_transfer NSArray    *)ABAddressBookCopyArrayOfAllPeople(addressBook);

        NSUInteger i = 0;
        for (i = 0; i < [allContacts count]; i++)
        {
            ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
            NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
            NSString *lastName =  (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
            if (lastName == nil) {
                strLastname = @"";
            }
            else
                strLastname = lastName;

            NSString *fullName = [NSString stringWithFormat:@"%@ %@", firstName, strLastname];
            NSString *phone=nil;
            ABMultiValueRef phoneNumbers = ABRecordCopyValue(contactPerson,                                                                                kABPersonPhoneProperty);
            if (ABMultiValueGetCount(phoneNumbers) > 0) {
                phone = (__bridge_transfer NSString*)
                ABMultiValueCopyValueAtIndex(phoneNumbers, 0);
            } else {
                phone = @"[None]";
            }
            [arrContactName addObject:fullName];
            [arrPhoneNumber addObject:phone];
            NSLog(@"Full Name =%@",fullName);
            NSLog(@"Phone Number =%@",phone);
        }
        CFRelease(addressBook);
    }
}

希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36348828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档