Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Fetch the Contact Detail Using iPhone SDK

    • 0
    • 2
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 575
    Comment on it

    In iPhone application development it is required to fetch the contact detail that are saved in the contact list of iPhone. So here is the program that will help you to fetch the detail of the contact with name number and contact image, if the image is not found a default image will be used .

    This is a class where we are fetching the detail of the contact and can access them anywhere in the app where it is necessary to to use the contact detail.

    @interface Contact : NSObject
    
    @property (nonatomic,retain) NSString *firstName;
    @property (nonatomic,retain) NSString *lastName;
    @property(nonatomic,retain)NSString *mobileNumber;
    @property(nonatomic,retain)NSString *fullName;
    @property(nonatomic,readwrite)int contactId;
    @property(nonatomic,retain) UIImage *ContactImage;
    
    -(NSMutableArray *)getAllContacts;
    
    @end
    
    
    
    
    #import "Contact.h"
    #import <AddressBook/AddressBook.h>
    
    @implementation Contact
    
    -(NSDictionary *)ContactListMapping
    {
        return [[NSDictionary alloc]initWithObjectsAndKeys:@"firstName",@"firstname",            @"lastName",@"lastname",@"mobileNumber",@"phoneNumber",@"fullName",@"fullname",@"contactId",@"contactid",@"contactImage",@"imgData,nil];
    }
    
    -(NSMutableArray *)getAllContacts
    {
            CFErrorRef *error = nil;
            ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
            ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
            CFArrayRef allPeople = (ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName));
                    CFIndex nPeople = CFArrayGetCount(allPeople); 
    
    NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];
    
            if (!allPeople || !nPeople) {
                NSLog(@"people nil");
            }
            for (int i = 0; i < nPeople; i++) {
    //data model
      Contact *contacts = [Contact new];
    ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
    
     //get First Name
      CFStringRef firstName = (CFStringRef)ABRecordCopyValue(person,kABPersonFirstNameProperty);
       contacts.firstName = [(__bridge NSString*)firstName copy];
    
       if (firstName != NULL) {
                        CFRelease(firstName);
                    }
    //get Last Name
      CFStringRef lastName = (CFStringRef)ABRecordCopyValue(person,kABPersonLastNameProperty);
                    contacts.lastName = [(__bridge NSString*)lastName copy];
    
                    if (lastName != NULL) {
                        CFRelease(lastName);
                    }
                    if (!contacts.firstName) {
                        contacts.firstName = @"";
                    }
    if (!contacts.lastName) {
                        contacts.lastName = @"";
                    }
    contacts.contactId = ABRecordGetRecordID(person) ;
                    //Get the full name of contact
     contacts.fullName = [NSString stringWithFormat:@"%@ %@", contacts.firstName, contacts.lastName];
    // get contacts picture, if pic doesn't exists, show standart one here I am using a image ConatactImage.png 
                    CFDataRef imgData = ABPersonCopyImageData(person);
                    NSData *imageData = (__bridge NSData *)imgData;
    contacts.contactImage = [UIImage imageWithData:imageData];
    if (imgData != NULL) {
                        CFRelease(imgData);
      }
    if (!contacts.ContactImage) {
                        contacts.ContactImage = [UIImage imageNamed:@"ConatactImage.png"];
                    }
    //get Phone Numbers
                    NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
                    ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
    
                    for(CFIndex i=0; i<ABMultiValueGetCount(multiPhones); i++) {
                        @autoreleasepool {
                            CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
                            NSString *phoneNumber = CFBridgingRelease(phoneNumberRef);
                            if (phoneNumber != nil){
                               [phoneNumbers addObject:phoneNumber];
    
                                contacts.mobileNumber=[phoneNumber copy];
    
                            }
    }
                    }
    
                    if (multiPhones != NULL) {
                        CFRelease(multiPhones);
                    }
                    [items addObject:contacts];
    
            } //autoreleasepool
            CFRelease(allPeople);
            CFRelease(addressBook);
            CFRelease(source);
    
            return items;
    
        }
    
    @end
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: