如何从联系人框架导入联系人 ios9

How to import contacts from the contact Framework ios9

如何使用联系人框架添加多个 phone 号码 iOS 9

CNMutableContact *contact = [test mutableCopy];
CNLabeledValue *homePhone_1 = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:[CNPhoneNumber phoneNumberWithStringValue:@"019312-555-1212"]];
CNLabeledValue * homePhone_2 = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:[CNPhoneNumber phoneNumberWithStringValue:@"312-555-1219"]];
[contact.phoneNumbers addObjectsFromArray:@[homePhone_1]];
[contact.phoneNumbers addObjectsFromArray:@[homePhone_2]];
CNSaveRequest *request = [[CNSaveRequest alloc] init];
[request updateContact:contact];

请大家帮忙。这不起作用。

请尝试以下code

- (void) addContact {

    CNMutableContact * contact = [CNMutableContact new];
    contact.middleName = @"Testmiddle";
    contact.contactType = CNContactTypePerson;
    contact.givenName = @"TestGivenname";
    contact.familyName = @"Taken";
    CNLabeledValue *homePhone_1 = [CNLabeledValue labeledValueWithLabel:CNLabelHome value:[CNPhoneNumber phoneNumberWithStringValue:@"019312-555-1212"]];
    CNLabeledValue * homePhone_2 = [CNLabeledValue labeledValueWithLabel:CNLabelWork value:[CNPhoneNumber phoneNumberWithStringValue:@"312-555-1219"]];
    contact.phoneNumbers = @[homePhone_1, homePhone_2];
    CNSaveRequest *request = [[CNSaveRequest alloc] init];
    [request addContact:contact toContainerWithIdentifier:nil];

    @try {
        CNContactStore * store = [CNContactStore new];
        [store executeSaveRequest:request error:nil];
    }
    @catch (NSException *exception) {
        NSLog(@"description =  %@",[exception description]);

    }
}

对于 update 联系人,您需要使用以下 code.

获取联系人封装 I/O 操作,所以我建议您在后台线程上使用它们。如果需要,您可以安全地将不可变的获取结果发送回主线程

- (void)updateContact {

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{

        CNContactStore * store = [CNContactStore new];
        NSArray * arrFetchedcontact = [NSArray array];

        @try {
            arrFetchedcontact = [store unifiedContactsMatchingPredicate:[CNContact predicateForContactsMatchingName:@"TestGivenname"] keysToFetch:[NSArray arrayWithObjects:@"CNContactGivenNameKey",@"CNContactFamilyNameKey",CNContactPhoneNumbersKey, nil] error:nil];
        }
        @catch (NSException *exception) {
            NSLog(@"description =  %@",[exception description]);
        }

        dispatch_async(dispatch_get_main_queue(), ^{

            if([arrFetchedcontact count] > 0){

                CNMutableContact * contact = [[arrFetchedcontact objectAtIndex:0] mutableCopy];
                NSMutableArray * arrNumbers = [[contact phoneNumbers] mutableCopy];
                CNLabeledValue * homePhone_2 = [CNLabeledValue labeledValueWithLabel:CNLabelOther value:[CNPhoneNumber phoneNumberWithStringValue:@"33333333333"]];
                [arrNumbers addObject:homePhone_2];

                contact.phoneNumbers = arrNumbers;

                CNSaveRequest *request = [[CNSaveRequest alloc] init];
                [request updateContact:contact];

                @try {
                    [store executeSaveRequest:request error:nil];
                }
                @catch (NSException *exception) {
                    NSLog(@"description =  %@",[exception description]);
                }
            }
        });
    });
}