添加人员组到ABAddressBookRef,组添加确定,但是人没有添加到组?
我有一些Objective-C代码,如:
ABAddressBookRef addressBook; CFErrorRef error = NULL; addressBook = ABAddressBookCreate(); ABRecordRef group = ABGroupCreate(); ABRecordSetValue(group, kABGroupNameProperty,@"My Group", &error); ABRecordRef person = ABPersonCreate(); ABRecordSetValue(person,kABPersonFirstNameProperty,@"Huy 1111111",&error); ABRecordSetValue(person,kABPersonLastNameProperty,@"AseDra",&error); ABGroupAddMember(group, person, &error); ABAddressBookAddRecord(addressBook, group, &error); ABAddressBookSave(addressBook,&error);
它运行正常,没有错误,组添加到模拟器地址簿但人没有添加到Simunator地址簿。 谁能告诉我哪里错了?
根据iOS的地址簿编程指南(下面的链接),人员记录必须存在于地址簿中才能将其添加到组中。
“在将人员记录添加到组之前,它必须已保存到通讯簿数据库中。”
所以也许这会奏效
ABAddressBookRef addressBook; CFErrorRef error = NULL; addressBook = ABAddressBookCreate(); ABRecordRef person = ABPersonCreate(); ABRecordSetValue(person,kABPersonFirstNameProperty,@"Huy 1111111",&error); ABRecordSetValue(person,kABPersonLastNameProperty,@"AseDra",&error); // add the person record ABAddressBookAddRecord(addressBook, person, &error); ABRecordRef group = ABGroupCreate(); ABRecordSetValue(group, kABGroupNameProperty,@"My Group", &error); ABGroupAddMember(group, person, &error); ABAddressBookAddRecord(addressBook, group, &error); ABAddressBookSave(addressBook,&error);
我已经阅读了你必须首先将人员保存到地址簿,然后创建组然后将人员添加到组中,然后再次保存的答案。
这对我不起作用。
最终对我有用的是:
将联系人保存在通讯簿中,将组保存在通讯簿中,将联系人添加到组中,然后保存。 非常烦人,但很高兴它有效,并希望它适合你。
ABAddressBookRef addressBook; bool wantToSaveChanges = YES; bool didSave; CFErrorRef error = NULL; addressBook = ABAddressBookCreateWithOptions(NULL, &error); // If you get some error when trying to add a contact, make sure it's not a permission issue // ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) { // // }); // create person ABRecordRef aRecord = ABPersonCreate(); CFErrorRef anError = NULL; bool didSet; didSet = ABRecordSetValue(aRecord, kABPersonFirstNameProperty, CFSTR("Lily"), &anError); if (!didSet) { NSLog(@"Error: %@", anError); } didSet = ABRecordSetValue(aRecord, kABPersonLastNameProperty, CFSTR("Bell"), &anError); if (!didSet) { NSLog(@"Error: %@", anError); } bool didAdd; didAdd = ABAddressBookAddRecord(addressBook, aRecord, &anError); if (!didAdd) { NSLog(@"Error: Couldn't add record, %@", anError); }; // we've added the person to the address book at this point // now try creating a group, and adding that person to the group (failed to add, error is NULL, for whatever reason) // if that fails, we might need to save the added contact, then add them to the group, then save (FAILED) // if the above fails, save the added contact, save the group, add the contact to group, then save (PASSED) // if the above fails, god help us // saving the added person first before adding them to the group didSave = ABAddressBookSave(addressBook, &error); if (!didSave) { NSLog(@"Error: %@", error); } ABRecordRef group = ABGroupCreate(); // create the group didSet = ABRecordSetValue(group, kABGroupNameProperty, @"NYC", &anError); if (!didSet) { NSLog(@"Error: Couldn't set group, %@", anError); } didAdd = ABAddressBookAddRecord(addressBook, group, &anError); if (!didAdd) { NSLog(@"Error: Couldn't add group, %@", anError); } // save the group now didSave = ABAddressBookSave(addressBook, &error); if (!didSave) { NSLog(@"Error: %@", error); } // adding person to group didAdd = ABGroupAddMember(group, aRecord, &anError); if (!didAdd) { NSLog(@"Error: Couldn't add contact to group, %@", anError); } // final save didSave = ABAddressBookSave(addressBook, &error); if (!didSave) { NSLog(@"Error: %@", error); } CFRelease(addressBook); CFRelease(aRecord); CFRelease(firstName); CFRelease(lastName);