0

私はiOSを初めて使用し、アプリで連絡先を取得しましたが、連絡先IDを取得する方法...同じ名前で保存された2つの番号がある場合、その特定の連絡先名のIDを取得する方法は?

4

1 に答える 1

1

あなたはこのように試すことができます: フレームワークのインポート

#import <Contacts/Contacts.h>

コード

- (void) getContacts {
    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 {
                for (CNContact *contact in cnContacts) {
                    //store all the contacts as per your requirement
                    NSLog(@"Id %@",contact.identifier);//the contact id which you want
                    NSLog(@"Name %@",contact.givenName);
                }
            }
        }        
    }];
}
于 2016-04-27T16:15:29.617 に答える