如何打开特定联系人的编辑联系人屏幕

How to open Edit Contacts Screen for Specific Contact

我正在开发一个 iOS 应用程序,我必须在其中 add 联系 Address Book

我想打开 Edit 联系人屏幕每当用户尝试添加 duplicate 联系人时。

但是我不知道怎么办that.Currently我只能显示一条消息

我正在获取所有联系人列表:

 NSArray *allContacts = (__bridge NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBookRef);

然后我迭代它并检查现有的 one.If 它存在然后我显示一条消息,否则我将把它添加到地址簿。

     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

是否可以这样做或者not.Please任何人如果可行的话可以帮助我实现这个功能。

您可以按如下方式编辑联系人

这里要加上

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

看这里

但是我还没有找到解决办法,如果你有请告诉我

看这里.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

中查看

答案在这里 当 bind arrayOfContact 的时候必须 Provide key with [CNContactViewController descriptorForRequiredKeys].

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