get Contact with cncontactstore from Facebook or gmail or any other cloud iOS

779 Views Asked by At

Currently I am using this code snippet to fetch contact from iPhone

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

    [store requestAccessForEntityType:CNEntityTypeContacts completionHandler:^(BOOL granted, NSError * _Nullable error) {
        if (granted == YES) {
            //keys with fetching properties
            NSArray *keys = @[CNContactGivenNameKey,CNContactFamilyNameKey,  CNContactPhoneNumbersKey, CNContactImageDataKey];

            [[CNContactsUserDefaults sharedDefaults] sortOrder];
            NSString *containerId = store.defaultContainerIdentifier;
            //NSComparator hg=   [CNContact comparatorForNameSortOrder:CNContactSortOrderGivenName];

            NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:containerId];
            NSError *error;
            NSArray *cnContacts = [store unifiedContactsMatchingPredicate:predicate keysToFetch:keys error:&error];

            NSSortDescriptor *sortDescriptor;
            sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"givenName"
                                                         ascending:YES];
            NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
            NSArray *sortedArray = [cnContacts sortedArrayUsingDescriptors:sortDescriptors];

}];

but here i am only getting contacts from iCloud.

how can i get contact from Facebook, gmail, or any other cloud? please help regard this.

2

There are 2 best solutions below

0
On

I spent 2 days for this issue but didn't get any solution.

Then done that with one patch. This is worked for me.

if ([container.name isEqualToString:@"Card"]) {
                    NSLog(@"iCloud");
                }else if ([container.name isEqualToString:@"Address Book"]){
                    NSLog(@"google");
                }else if ([container.name isEqualToString:@"Contacts"]){
                    NSLog(@"Yahoo");
                }else{
                    NSLog(@"Other");
                }
0
On

You have to fetch all containers for contacts and fetch contact from individual container and merge them.

// Get all containers of contacts like icloud contacts , gmail contacts and show all data
    NSArray *allContainers = [store containersMatchingPredicate:nil error:nil];

for (CNContainer *container in allContainers) {
        // create predicate using cncontactstore identifier
        NSPredicate *predicate = [CNContact predicateForContactsInContainerWithIdentifier:container.identifier];

        // get array of contact using predicate
        NSArray *arrContacts = [[[CNContactStore alloc] init] unifiedContactsMatchingPredicate:predicate keysToFetch:keysToFetch error:nil];
    }