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

时间:2015-11-20 05:12:43

标签: ios objective-c abaddressbook

我正在处理一个iOS申请,我必须在add Address Book中与Edit个人联系。

我想打开duplicate联系人屏幕每当用户尝试添加 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");
    }
}

然后我正在通过它来检查现有的。如果它存在,那么我正在显示一条消息,否则我会将它添加到地址簿中。

numbers.pop(index - 1)

当用户尝试添加重复的联系人时,如何打开以下屏幕:

enter image description here

我搜索了SO并找到了以下问题,但这对我没有帮助。 Question 1 Question 2

是否有可能这样做。如果可行,请任何人协助我实现此功能。

3 个答案:

答案 0 :(得分:2)

请参阅此处.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

答案 1 :(得分:1)

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

在这里你必须添加

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

但我仍然没有找到解决方案,如果你有请告诉我

答案 2 :(得分:0)

这是答案 当绑定arrayOfContact时,必须提供带有[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];
相关问题