gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:21:11 30 4
gpt4 key购买 nike

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

请大家帮忙。这是行不通的。

最佳答案

请尝试使用以下代码

- (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]);

}
}

对于更新,您需要使用以下代码

Fetching contact 封装了 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]);
}
}
});
});
}

关于ios - 如何从联系人框架ios9导入联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33513738/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com