名前と電話帳にアクセスしようとして数日間苦しみ、すべて失敗しました。テストアプリケーションで正常に動作する次のコードを使用しますが、プロジェクトワークに追加すると機能しません。変数 "granted"には常に値 "false"があり、エラー "Access Failure"が発生します。これにもかかわらず、プライバシー設定でアクセスを許可するスライダを表示しません... 私は長い間、むしろ奇妙な行動への答えを見つけることができませんでしたしている...アプリケーションが電話帳にアクセスできない
私は任意の助けいただければ幸いです! `
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に 'Privacy - Contacts Usage Description'キーを追加しましたか? iOS 6.0以降でサポートされています。 –
私はiOSを試しました8と9はまだ動作しません –