Search This Blog

Thursday 6 December 2012

Fetch Contacts in iOS 6.0 and Lower versions iPhone,iOS

Hi All,

Here I will show you how iOS 6.0 and its lower version differs in AddressBookRef to fetch the contacts.I am going to explain you with the code.Following is the explanation,
This code works on iOS 6.0 as well as on lower versions to fetch the contacts.

We need to declare the macro which stores the iOS version of connected device.
Following macros does this,


#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)

after declaring macros whenever you want to fetch the contacts,just write below code,
-(void) didLoad {

// It compares whether current iOS version is than 6.0  
  if (SYSTEM_VERSION_LESS_THAN(@"6.0")) {
        NSLog(@"<6.0");
       
        //Code to fetch Contacts from lower version of iOS

        ABAddressBookRef UsersAddressBook = ABAddressBookCreate();
        
        
        
        //contains details for all the contacts
        CFArrayRef ContactInfoArray = ABAddressBookCopyArrayOfAllPeople(UsersAddressBook);
        
        //get the total number of count of the users contact
        CFIndex numberofPeople = CFArrayGetCount(ContactInfoArray);
        
        //iterate through each record and add the value in the array
        for (int i =0; i<numberofPeople; i++) {
            ABRecordRef ref = CFArrayGetValueAtIndex(ContactInfoArray, i);
            ABMultiValueRef names = (NSString*)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
            ABMultiValueRef lastname=(NSString*)ABRecordCopyValue(ref, kABPersonLastNameProperty);
            
            ABMultiValueRef emailMultiValue = ABRecordCopyValue(ref, kABPersonEmailProperty);
            NSArray *emailAddresses = [(NSArray *)ABMultiValueCopyArrayOfAllValues(emailMultiValue) autorelease];
            
            
            ABMultiValueRef multi = ABRecordCopyValue(ref, kABPersonPhoneProperty);
            
            NSArray *no2 = [(NSArray*)ABMultiValueCopyArrayOfAllValues(multi) autorelease];
            
            ABMultiValueRef fullName=[NSString stringWithFormat:@"%@ %@",names,lastname];
          
            
            for (int i=0; i<[no2 count]; i++) {
                [arr_name addObject:fullName];
                [arr_ContactsInfo addObject:[no2 objectAtIndex:i]];
            }
            for (int i=0; i<[emailAddresses count]; i++) {
                [arr_name addObject:fullName];
                [arr_ContactsInfo addObject:[emailAddresses objectAtIndex:i]];
            }
            
            
        }

    }else if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
        NSLog(@">=6.0");

        //When Current iOS >= 6.0 
        ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);
        
        if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
            ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
                // First time access has been granted, add the contact
                //contains details for all the contacts
                CFArrayRef ContactInfoArray = ABAddressBookCopyArrayOfAllPeople(addressBookRef);
                
                //get the total number of count of the users contact
                CFIndex numberofPeople = CFArrayGetCount(ContactInfoArray);
                
                //iterate through each record and add the value in the array
                for (int i =0; i<numberofPeople; i++) {
                    ABRecordRef ref = CFArrayGetValueAtIndex(ContactInfoArray, i);
                    ABMultiValueRef names = (NSString*)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
                    ABMultiValueRef lastname=(NSString*)ABRecordCopyValue(ref, kABPersonLastNameProperty);
                    
                    ABMultiValueRef emailMultiValue = ABRecordCopyValue(ref, kABPersonEmailProperty);
                    NSArray *emailAddresses = [(NSArray *)ABMultiValueCopyArrayOfAllValues(emailMultiValue) autorelease];
                    //    CFRelease(emailMultiValue);
                    
                    
                    ABMultiValueRef multi = ABRecordCopyValue(ref, kABPersonPhoneProperty);
                    
                    //  ABMultiValueRef no = (NSString*)ABMultiValueCopyValueAtIndex(multi, 0);
                    NSArray *no2 = [(NSArray*)ABMultiValueCopyArrayOfAllValues(multi) autorelease];
                                       
                    ABMultiValueRef fullName=[NSString stringWithFormat:@"%@ %@",names,lastname];
                    // [arr_name addObject:fullName];
                    //[arr_name addObject:fullName];
                    
                    for (int i=0; i<[no2 count]; i++) {
                        [arr_name addObject:fullName];
                        [arr_ContactsInfo addObject:[no2 objectAtIndex:i]];
                    }
                    for (int i=0; i<[emailAddresses count]; i++) {
                        [arr_name addObject:fullName];
                        [arr_ContactsInfo addObject:[emailAddresses objectAtIndex:i]];
                    }
                    
                    
                    
                }
                
            });
        }
        
     }
     else {
            // The user has previously denied access
            // Send an alert telling user to change privacy setting in settings app
        }


    }

}
If you run the code on iOS < 6.0 it will run as normally and will store the names in arr_name and arr_ContactsInfo array but if you run this code in iOS >= 6.0,it will will ask for the permission from the user whether to allow to fetch contacts.If permission granted,then will store the data in those array.

Happy coding :)