我忍受了好几天,试图访问名称和电话号码簿,全部失败。我使用了以下代码,该代码在测试应用程序中成功运行,但是当我将它添加到项目工作中时,它不起作用。 “授予”变量不断具有值 - “错误”,并且出现“访问失败”错误。尽管如此,在隐私设置不显示滑块允许访问... 我早已找不到的答案,一个相当奇怪的行为...应用程序无法访问电话簿
我会感谢任何帮助!`
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
if (granted == YES) {
NSMutableArray *contacts = [NSMutableArray array];
NSArray *keys = @[CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey, CNContactImageDataKey];
NSString *containerId = store.defaultContainerIdentifier;
NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
NSError *error;
NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];
if (error) {
NSLog(@"error fetching contacts %@", error);
} else {
for (CNContact *contact in cnContacts) {
TSContact *newContact = [[TSContact alloc] init];
newContact.firstName = contact.givenName;
newContact.lastName = contact.familyName;
UIImage *image = [UIImage imageWithData:contact.imageData];
newContact.image = image;
for (CNLabeledValue *label in contact.phoneNumbers) {
NSString *phone = [label.value stringValue];
if ([phone length] > 0) {
[contacts addObject:phone];
}
}
}
}
} else {
NSLog(@"Error = %@", error.localizedDescription);
}
}];
什么是您的应用程序的部署目标(目标iOS版本6.0的东西)? – theFool
您是否在info.plist中添加了“隐私 - 联系人使用说明”键?它支持iOS 6.0及更高版本。 –
我试过iOS ios 8和9仍然不起作用 –