我正在處理我的應用程序和iPhone的AddressBook之間的一些集成。這是我的程序的流程。聯繫人被選中後從AddressBook中刪除
- 用戶希望導入聯繫人
- 應用介紹「的ABPeoplePickerNavigationController」給用戶。
- 用戶選擇他們想要的聯繫人:
- 委託方法被稱爲:
下面是代碼:
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
shouldContinueAfterSelectingPerson:(ABRecordRef)person {
self.selectedContact = person;
[self showNewContactViewThroughController:peoplePicker withRecord:person];
NSLog(@"should continue after selecting");
return NO;
}
5:- (void)showNewContactViewThroughController:withRecord:
我們創建AbNewPersonViewController。
`ABNewPersonViewController *newController = [[ABNewPersonViewController alloc] init];`
`newController.displayedPerson = person;`
`newController.newPersonViewDelegate = self;`
然後按下它。
6:用戶點擊「取消」退出ABNewPersonViewController視圖。
7:選中聯繫人應用程序,他們在步驟3中選擇的聯繫人消失。噗,走了,刪除,刪除。
爲了解決這個問題,我保存了ABRecordRef(實例變量「selectedContact」)。然後,在- (void)newPersonViewController:didCompleteWithNewPerson:
我:
if (person) {
//do stuff with the person
else {
///
/// This means they canceled, so we need to save the old person back
///
if (self.selectedContact) {
ABAddressBookRef addressBook = ABAddressBookCreate();
CFErrorRef error = NULL;
ABAddressBookAddRecord(addressBook, self.selectedContact, &error);
if (error != NULL) {
NSLog(@"Error Adding Record: %@", error);
}
ABAddressBookSave(addressBook, &error);
if (error != NULL) {
NSLog(@"AccountDetailTableViewController.newPersonViewController() The old contact was not saved successfully. %@", error);
}
self.selectedContact = nil;
}
}
然而,這似乎並沒有做任何事情。該代碼已執行,但「舊聯繫人」self.selectedContact未保存到AddressBook。所以,我的聯繫人仍然在消失。我究竟做錯了什麼?看起來好像你在ABNewPersonViewController中點擊了Cancel,它會「刪除」從地址簿中給出的數據?那麼我給它的人死了?任何幫助,將不勝感激!