简体   繁体   English

联系人框架中缺少联系人(Objective-C)

[英]Missing Contacts from Contacts Framework (Objective-C)

For some reason, a few of my contacts are missing from my tableView. 出于某种原因,我的tableView中缺少一些联系人。 I believe am I fetching all the necessary keys and have reviewed Apple's documentation, but am still stuck. 我相信我正在获取所有必要的密钥,并且已经查看了Apple的文档,但仍然卡住了。 Below is my code: 以下是我的代码:

  -(void)fetchContactsandAuthorization {
// Request authorization to Contacts
CNContactStore *store = [[CNContactStore alloc] init];
[store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
    if (granted == YES)
    {

        //keys with fetching properties
        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
        {
            NSString *phone;
            NSString *fullName;
            NSString *firstName;
            NSString *lastName;
            UIImage *profileImage;
            NSMutableArray *contactNumbersArray = [[NSMutableArray alloc]init];
            for (CNContact *contact in cnContacts) {
                // copy data to my custom Contacts class.
                firstName = contact.givenName;
                lastName = contact.familyName;
                if (lastName == nil) {
                    fullName=[NSString stringWithFormat:@"%@",firstName];
                }else if (firstName == nil){
                    fullName=[NSString stringWithFormat:@"%@",lastName];
                }
                else{
                    fullName=[NSString stringWithFormat:@"%@ %@",firstName,lastName];
                }

                UIImage *image = [UIImage imageWithData:contact.imageData];
                if (image != nil) {
                    profileImage = image;
                }else{
                    profileImage = [UIImage imageNamed:@"person-icon.png"];
                }

                NSString *number = contact.phoneNumbers.firstObject.value.stringValue;

                if (number == (NSString *)NSNull.null || number == nil) {
                    [contactNumbersArray addObject:@"NO NUMBER"];
                } else {
                    [contactNumbersArray addObject:number];
                }


                arrayPhoneData = contactNumbersArray;
                NSDictionary* personDict = [[NSDictionary alloc] initWithObjectsAndKeys: fullName,@"fullName",profileImage,@"userImage",phone,@"PhoneNumbers", nil];
                [arrayTableData addObject:[NSString stringWithFormat:@"%@",[personDict objectForKey:@"fullName"]]];
            }
            dispatch_async(dispatch_get_main_queue(), ^{
                //[tableViewContactData reloadData];
                [self.tableView reloadData];
            });
        }
      }
  }];
}

Any help is much appreciated!!!! 任何帮助深表感谢!!!!

`
let keys = [CNContactGivenNameKey, CNContactPhoneNumbersKey] as [Any]

        do {
            let contactStore = AppDelegate.getAppDelegate().contactStore
            try contactStore.enumerateContacts(with: CNContactFetchRequest(keysToFetch: keys as! [CNKeyDescriptor])) { (contact, pointer) -> Void in
                if !contact.phoneNumbers.isEmpty {
                    contacts.append(contact)
                }
            }
        }
        catch let error as NSError {

            print(error.description, separator: "", terminator: "\n")
        }


        let contactPickerViewController = CNContactPickerViewController()

        contactPickerViewController.delegate = self

        present(contactPickerViewController, animated: true, completion: nil)
`

stick this in your app delegate 坚持你的应用程序代表

var contactStore = CNContactStore()

   class func getAppDelegate() -> AppDelegate {
        return UIApplication.shared.delegate as! AppDelegate
    }

Try double checking your predicate. 尝试仔细检查您的谓词。 I have had trouble with the predicate restricting the contacts I have been searching for and removed it entirely in the past. 我的谓词限制了我一直在搜索的联系人,并在过去完全删除了。 Not also that but you could try adding more keys to your contact keys. 不是那样,但您可以尝试在联系人密钥中添加更多密钥。 You're potentially restricting your search with keysToFetch. 您可能会使用keysToFetch限制搜索。

You should access the Contact Book differently. 您应该以不同方式访问联系人簿。

   dispatch_async(dispatch_get_main_queue(), ^{

    CNContactStore *store = [[CNContactStore alloc] init];

    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {

        if (granted == NO) {
            //show access denied popup
            return ;
        }


        NSError* contactError;
        CNContactStore* addressBook = [[CNContactStore alloc]init];
        [addressBook containersMatchingPredicate:[CNContainer predicateForContainersWithIdentifiers: @[addressBook.defaultContainerIdentifier]] error:&contactError];
        NSArray *keysToFetch = @[CNContactMiddleNameKey,CNContactFamilyNameKey, CNContactGivenNameKey, CNContactPhoneNumbersKey,CNContactEmailAddressesKey];
        CNContactFetchRequest * request = [[CNContactFetchRequest alloc]initWithKeysToFetch:keysToFetch];


        [addressBook enumerateContactsWithFetchRequest:request error:&contactError usingBlock:^(CNContact * __nonnull contact, BOOL * __nonnull stop){\

            NSString *firstName = contact.givenName;
            NSString *lastName = contact.familyName;

           //recommend to use a function like this one: [self parseContactWithContact:contact];
        }];


    }];
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 iOS / objective-c:Facebook整合无法再导入联系人? - iOS/objective-c: Facebook integration importing contacts no longer possible? 在iPhone上的Objective-C中查找联系人图像的路径 - Finding the path to a contacts image in Objective-C on the iPhone 访问联系人iOS目标c - Access Contacts iOS objective c IOS Objective-c - 获取联系人不起作用 - 没有请求提示 - 拒绝访问 - IOS Objective-c - Getting contacts not working - No request prompt - Access denied 优化Objective C代码以访问联系人 - Optimize Objective C code to access contacts 如何在tableview中显示联系人 - 目标C. - How to display contacts in tableview - Objective C Objective-C应用程序中缺少Swift框架中的IBOutlet属性 - IBOutlet properties from Swift framework are missing from Objective-C app 在目标c中使用联系人框架时,无法在表视图中显示联系人缩略图 - Unable to show contact thumbnail image in tableview while using contacts framework in objective c Google Contacts API 3.0版是否可以检索目标C(iOS)示例请求格式的所有联系人? - Google Contacts API version 3.0 to retrieve all contacts for objective C (iOS) sample request format? iOS9联系人框架缺少电话号码标签? - iOS9 Contacts Framework missing phone number labels?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM