English 中文(简体)
iPhone:add, update and remove contact programmatically
原标题:

i am new to iPhone programming i want to ask that whether i can add, update or remove contact from iPhone contact list. Please suggest or provide me some helpful link.

最佳回答

Apple has extensive docs on accessing the Address Book in general, and manipulating contacts. It s C-based, rather than Objective-C, however.

问题回答

Remove Contact:

     - (void) deleteAllContacts {
CNContactStore *contactStore = [[CNContactStore alloc] init];

[contactStore requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (granted == YES) {
        NSArray *keys = @[CNContactPhoneNumbersKey];
        NSString *containerId = contactStore.defaultContainerIdentifier;
        NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
        NSError *error;
        NSArray *cnContacts = [contactStore unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];

        if (error) {
            NSLog(@"error fetching contacts %@", error);
        } else {
            CNSaveRequest *saveRequest = [[CNSaveRequest alloc] init];

            for (CNContact *contact in cnContacts) {
     //Here write the necessary condition to filter the contacts which you want to delete .  
                [saveRequest deleteContact:[contact mutableCopy]];
            }

            [contactStore executeSaveRequest:saveRequest error:nil];
            DDLogVerbose(@"Deleted contacts %lu", cnContacts.count);
        }
    }
}];

}

Add Contact:

    CNAuthorizationStatus status = [CNContactStore authorizationStatusForEntityType:CNEntityTypeContacts];
if (status == CNAuthorizationStatusDenied || status == CNAuthorizationStatusRestricted) {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Access to contacts" message:@"This app requires access to contacts" preferredStyle:UIAlertControllerStyleActionSheet];
    [alert addAction:[UIAlertAction actionWithTitle:@"Go to settings" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil]];
    [self presentViewController:alert animated:TRUE completion:nil];
    return;
}
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (!granted) {
        dispatch_async(dispatch_get_main_queue(), ^{
            //user didn t grant access
        });return;}
    //create contact
    CNMutableContact *contact = [[CNMutableContact alloc] init];
    NSMutableString *temp = [[NSMutableString alloc] initWithString:newContactName];
    [temp appendString:@"(PATIENT)"];
    NSString *immutable = [NSString stringWithString:temp];
    contact.givenName = immutable;

    CNLabeledValue *homePhone = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:[CNPhoneNumber phoneNumberWithStringValue:mobileNumberOfPatient]];
    contact.phoneNumbers = @[homePhone];
    CNSaveRequest *request = [[CNSaveRequest alloc] init];
    [request addContact:contact toContainerWithIdentifier:nil];
    [self.activityIndicator setHidden:YES];
    [self.activityIndicator stopAnimating];
    [self shareViaWhatsApp];
    //save it
    NSError *saveError;
    if (![store executeSaveRequest:request error:&saveError]) {
        NSLog(@"error: %@",saveError);
    }
}];




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签