如何打开特定联系人的编辑联系人屏幕
Posted
技术标签:
【中文标题】如何打开特定联系人的编辑联系人屏幕【英文标题】:How to open Edit Contacts Screen for Specific Contact 【发布时间】:2015-11-20 05:12:43 【问题描述】:我正在开发一个ios
应用程序,我必须在其中add
联系人Address Book
。
每当用户尝试添加duplicate
联系人时,我想打开Edit
联系人屏幕。
但我不知道该怎么做。目前我只能显示一条消息。
我将所有联系人列表设为:
NSArray *allContacts = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBookRef);
然后我将遍历它并检查是否存在。如果存在,那么我将显示一条消息,否则我会将其添加到地址簿中。
for (id record in allContacts)
ABRecordRef thisContact = (__bridge ABRecordRef)record;
if (CFStringCompare(ABRecordCopyCompositeName(thisContact),
ABRecordCopyCompositeName(pet), 0) == kCFCompareEqualTo)
//The contact already exists!
NSLog(@"contact exosts");
else
ABAddressBookAddRecord(addressBookRef, pet, nil);
ABAddressBookSave(addressBookRef, nil);
ABMultiValueAddValueAndLabel(phoneNumbers, (__bridge CFStringRef)petPhoneNumber, kABPersonPhoneMainLabel, NULL);
NSLog(@"contacts Added");
当用户尝试添加重复联系人时,如何打开以下屏幕:
我搜索了 SO 并找到以下问题,但这对我没有帮助。 Question 1 Question 2
是否可以这样做。如果可行,请任何人帮助我实现此功能。
【问题讨论】:
【参考方案1】:请看这里.h
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
@interface ContactsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, ABPersonViewControllerDelegate>
IBOutlet UITableView *tblContacts;
NSMutableArray *arrContacts;
@end
还有.m
#import "ContactsViewController.h"
@interface ContactsViewController ()
UIAlertController *action;
@end
@implementation ContactsViewController
#pragma mark - View lifecycle
- (void)viewDidLoad
[super viewDidLoad];
self.title = @"Contacts";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewContact:)];
[self getContactsUsingAddressbook];
#pragma mark - Methods
// ------- Deprecated (in iOS 9.0) ----------
- (void)getContactsUsingAddressbook
ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
if (status == kABAuthorizationStatusDenied || status == kABAuthorizationStatusRestricted)
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:@"This app previously was refused permissions to contacts; Please go to settings and grant permission to this app so it can use contacts" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:alert animated:TRUE completion:nil];
return;
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (!addressBook)
NSLog(@"ABAddressBookCreateWithOptions error: %@", CFBridgingRelease(error));
return;
ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error)
if (error)
NSLog(@"ABAddressBookRequestAccessWithCompletion error: %@", CFBridgingRelease(error));
if (granted)
NSArray *allPeople = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
arrContacts = [NSMutableArray arrayWithArray:allPeople];
[tblContacts reloadData];
else
dispatch_async(dispatch_get_main_queue(), ^
[[[UIAlertView alloc] initWithTitle:nil message:@"This app requires access to your contacts to function properly. Please visit to the \"Privacy\" section in the iPhone Settings app." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
);
CFRelease(addressBook);
);
#pragma mark - Tableview delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
return arrContacts.count;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//if (cell == nil)
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
cell.accessoryType = UITableViewCellAccessoryDetailButton;
// ------- Deprecated (in iOS 9.0) ----------
ABRecordRef person = (__bridge ABRecordRef)arrContacts[indexPath.row];
NSString *firstName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = CFBridgingRelease(ABRecordCopyValue(person, kABPersonLastNameProperty));
cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
return cell;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
// ------- Deprecated (in iOS 9.0) ----------
ABPersonViewController *personController = [[ABPersonViewController alloc] init];
personController.personViewDelegate = self;
personController.displayedPerson = (__bridge ABRecordRef)arrContacts[indexPath.row];
personController.allowsEditing = YES;
personController.allowsActions = YES;
[self.navigationController pushViewController:personController animated:TRUE];
#pragma mark - ABPersonview delegate
- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
return TRUE;
在my simulator中查看
【讨论】:
你好@cuteAngel...谢谢您的努力..您的代码运行良好..但是当我点击任何联系人时,它会显示另一个屏幕,其中包含联系方式..点击后编辑我被重定向到编辑页面......当我按下取消按钮时,在编辑页面中没有任何反应......并且是他们可以直接进入编辑页面的任何方式......【参考方案2】:您可以按如下方式编辑联系人
这里你要添加
// ------- Deprecated (in iOS 9.0)
#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
ABPersonViewController *personController = [[ABPersonViewController alloc] init];
personController.personViewDelegate = self;
personController.displayedPerson = (__bridge ABRecordRef)arrContacts[indexPath.row];
personController.allowsEditing = YES;
personController.allowsActions = YES;
[self.navigationController pushViewController:personController animated:TRUE];
这里
#import <Contacts/Contacts.h>
#import <ContactsUI/ContactsUI.h>
// -------- This is not working for me, I got error
CNContact *contact = [arrContacts objectAtIndex:indexPath.row];
NSArray *keys = @[CNContactIdentifierKey, CNContactEmailAddressesKey, CNContactBirthdayKey, CNContactImageDataKey, CNContactPhoneNumbersKey, [CNContactFormatter descriptorForRequiredKeysForStyle:CNContactFormatterStyleFullName]];
CNContactViewController *contactController = [CNContactViewController viewControllerForContact:contact];
contactController.delegate = self;
contactController.allowsEditing = YES;
contactController.allowsActions = YES;
contactController.displayedPropertyKeys = keys;
[self.navigationController pushViewController:contactController animated:TRUE];
请看这里Contact is missing some of the required key descriptors in ios
但是我还没有找到解决办法,如果有请告诉我
【讨论】:
让我试试这个...arrContacts[indexPath.row] 是要编辑的联系人索引吗? 我应该实现这两种方法还是任何一种? 其中任何一个,取决于您的 iOS 版本。如果您同时针对 iOS 8.0 和 iOS 9.0,则以条件方式实现两者 我已经实现了第一个但没有任何反应【参考方案3】:这就是答案 当绑定arrayOfContact的时候就得用[CNContactViewController descriptorForRequiredKeys]来提供key。
NSArray *keys = @[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactOrganizationNameKey, CNContactPhoneNumbersKey, CNContactEmailAddressesKey,CNContactPostalAddressesKey,[CNContactViewController descriptorForRequiredKeys]]
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
打开现有联系人时
CNContactViewController *contactController = [CNContactViewController viewControllerForContact:contact];
contactController.delegate = self;
contactController.allowsEditing = YES;
contactController.allowsActions = YES;
[self.navigationController pushViewController:contactController animated:TRUE];
【讨论】:
以上是关于如何打开特定联系人的编辑联系人屏幕的主要内容,如果未能解决你的问题,请参考以下文章
与特定联系人打开 LINE android 应用程序的聊天屏幕