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

时间:2015-11-04 04:31:49

标签: ios objective-c frameworks ios9 contact

如何使用Contact Framework 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];

请帮帮我们。这不起作用。

1 个答案:

答案 0 :(得分:5)

请尝试关注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]);
                }
            }
        });
    });
}