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

问题描述 投票:0回答:3

我正在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");
    }
}

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

enter image description here

我进行了搜索,发现了以下问题,但这对我没有帮助。Question 1Question 2

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

ios objective-c abaddressbook
3个回答
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
投票

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

这里您必须添加

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

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


0
投票

这里是答案当您绑定arrayOfContact时,您必须在[CNContactViewController descriptorForRequiredKeys]中提供密钥。

NSArray *keys = @[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactOrganizationNameKey, CNContactPhoneNumbersKey, CNContactEmailAddressesKey,CNContactPostalAddressesKey,[CNContactViewController descriptorForRequiredKeys]]
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];
© www.soinside.com 2019 - 2024. All rights reserved.