2015-11-20 479 views
0

我正在致力於iOS應用程序,其中我必須在Address Book中聯繫add聯繫人。如何打開特定聯繫人的編輯聯繫人屏幕

我想只要用戶嘗試添加duplicate接觸打開Edit聯繫人屏幕。

但我不知道該怎麼辦that.Currently我只能夠只顯示一條消息。

我得到的所有聯繫人列表如下:

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

然後我itterating通過它檢查現有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"); 
    } 
} 

我怎樣才能打開下面的畫面時用戶嘗試添加重複的聯繫人:

enter image description here

我搜索SO,找到下面的問題,但這並不能幫助我。 Question 1 Question 2

,並有可能這樣做或not.Please任何一個幫助我實現這個功能,如果它是可行的。

回答

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

+0

你好@cuteAngel ... Thnx爲你的努力噸..你的代碼工作很好..但是當我點擊任何聯繫它顯示我有另一個屏幕,有聯繫方式... ..點擊編輯後,我重定向到編輯頁面...和編輯頁面時,當我按取消按鈕什麼都沒有發生.... AND是他們的任何方式,我可以直接去編輯頁面... – Dalvik

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

讓我試試這一個... arrContacts [indexPath.row]是它接觸到索引被編輯? – Dalvik

+0

我應該實施這兩種方法還是任何一種? – Dalvik

+0

其中任何一個,取決於您的iOS版本。如果你的目標是iOS 8.0和iOS 9.0,那麼以條件的方式實現這兩個條件 – VRAwesome

相關問題