Rahul
Rahul

Reputation: 5844

How to open Edit Contacts Screen for Specific Contact

I am working on a iOS Application in which I have to add contacts in Address Book.

I want to open Edit Contact Screen Whenever user Tries to add duplicate contact.

But I don't know how to do that.Currently I am only able to show a message only.

I am getting all contacts list as:

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

Then I am itterating through it and check for existing one.If it exists then I am showing a message else I will add it to the addressbook.

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

How can I open following screen when user Tries to add duplicate contact:

enter image description here

I searched SO and find following questions but this doesn't help me. Question 1 Question 2

And Is it possible to do so or not.Please any one assist me to achieve this feature if it is feasible.

Upvotes: 0

Views: 1736

Answers (3)

ketan pawar
ketan pawar

Reputation: 1

Here is the answer when bind arrayOfContact That time have to Provide key with [CNContactViewController descriptorForRequiredKeys].

NSArray *keys = @[CNContactGivenNameKey,CNContactFamilyNameKey,CNContactOrganizationNameKey, CNContactPhoneNumbersKey, CNContactEmailAddressesKey,CNContactPostalAddressesKey,[CNContactViewController descriptorForRequiredKeys]]
CNContactFetchRequest *request = [[CNContactFetchRequest alloc] initWithKeysToFetch:keys];

when open existing Contact

  CNContactViewController *contactController = [CNContactViewController viewControllerForContact:contact];

  contactController.delegate               =   self;
  contactController.allowsEditing          =   YES;
  contactController.allowsActions          =   YES;
  [self.navigationController pushViewController:contactController animated:TRUE];

Upvotes: 0

VRAwesome
VRAwesome

Reputation: 4803

See here .h

#import <AddressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>

@interface ContactsViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, ABPersonViewControllerDelegate>
{
    IBOutlet UITableView *tblContacts;
    NSMutableArray *arrContacts;
}
@end

And .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;
}

And see in my simulator

Upvotes: 2

VRAwesome
VRAwesome

Reputation: 4803

You can edit contact as following

Here you have to add

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

And here

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

See here Contact is missing some of the required key descriptors in ios

But still I have not found solution , If you have please tell me

Upvotes: 1

Related Questions