Najeebullah Shah
Najeebullah Shah

Reputation: 3709

Goto address book/contact number directly from app

I want to make an app and want to access the contact numbers directly when i touch/press a particular text field or button and then return to my app with a selected contact number. How can i do this.

Upvotes: 0

Views: 400

Answers (3)

Priyanka Singh
Priyanka Singh

Reputation: 250

You need to add ABPeoplePickerNavigationControllerDelegate delegate in .h file

and in .m file write down below three methods:

#pragma mark People Picker Delegate Methods

- (void)peoplePickerNavigationControllerDidCancel:
  (ABPeoplePickerNavigationController *)peoplePicker {
[peoplePicker dismissModalViewControllerAnimated:YES];
[peoplePicker autorelease];
     }

- (BOOL)peoplePickerNavigationController:
  (ABPeoplePickerNavigationController *)peoplePicker 
  shouldContinueAfterSelectingPerson:(ABRecordRef)person {



return YES;
   }


- (BOOL)peoplePickerNavigationController:
(ABPeoplePickerNavigationController *)peoplePicker 
  shouldContinueAfterSelectingPerson:(ABRecordRef)person
                            property:(ABPropertyID)property
                          identifier:(ABMultiValueIdentifier)valueID{


ABPropertyType type = ABPersonGetTypeOfProperty(property);
if (type==kABMultiDictionaryPropertyType) {


    ABMutableMultiValueRef multi = ABRecordCopyValue(person, property);

    CFIndex index = ABMultiValueGetIndexForIdentifier(multi, valueID);
    CFDictionaryRef dic = ABMultiValueCopyValueAtIndex(multi, index);
    CFStringRef street = CFDictionaryGetValue(dic, kABPersonAddressStreetKey);

    NSString* StreetName =(NSString*)street;
    streetNameText.text=StreetName;
    NSLog(@"StreetName:%@",StreetName);
    NSRange range = NSMakeRange (0, 5);
    NSLog (@"Beer shortname: %@", [StreetName substringWithRange:range]);
    int val = [StreetName intValue];
    NSLog(@"StreetName:%d",val);
    NSString *newChange = [[NSString alloc] initWithFormat:@"%d", val];
    streetNOText.text = newChange;

    [newChange release];
    CFRelease(dic);
    CFRelease(multi);
}





[self dismissModalViewControllerAnimated:YES];
return NO;

     }

Upvotes: 1

NSException
NSException

Reputation: 1278

May this method will help you, just call this method when you want to fetch records from addressbook. n ofcourse add the frameworks AddressBook, AddressBookUI

first create database (as i did if you want you can store it into NSMutable Array instead of database, as per your requirement.)

-(void)fetchRecordsFromAddressBook
{
ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

//NSArray *addresses = (NSArray *) ABAddressBookCopyArrayOfAllPeople(addressBook);

//  [arrayContacts removeAllObjects];

[self emptyDataContext];

for (int i = 0; i < nPeople; i++)
{


    ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);


    //////////////////  get first name  ///////////////////

    CFStringRef firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);

    CFStringRef lastName = ABRecordCopyValue(ref, kABPersonLastNameProperty);

    CFStringRef nickName = ABRecordCopyValue(ref, kABPersonNicknameProperty);

    CFStringRef middleName = ABRecordCopyValue(ref, kABPersonMiddleNameProperty);


    //////////////////  get image  ///////////////////

//      ABMultiValueRef ContactImage = (ABMultiValueRef) ABRecordCopyValue(ref,kABPersonImageFormatThumbnail);


    NSData *data=nil;

//  NSLog(@"Image Testing is : %@",ref);

    if(ABPersonHasImageData(ref))
    {
        data = [(NSData *) ABPersonCopyImageData(ref) autorelease];
        if(data)
        {
        //  NSLog(@"Im Testing is : %@",data);

            //image = [[UIImage alloc] initWithData:data];
        }
    }

//      NSLog(@"Image is : %@",ContactImage);
//      NSLog(@" Name is : %@",firstName);



    //////////////////  get email  ///////////////////

    ABMultiValueRef emails = (ABMultiValueRef) ABRecordCopyValue(ref, kABPersonEmailProperty);

    NSString *emailID=@"";

    if(ABMultiValueGetCount(emails)>=1)
    {
        emailID = (NSString *)ABMultiValueCopyValueAtIndex(emails,0);
    }


    //////////////////  get phone number  ///////////////////

    ABMultiValueRef phones = (ABMultiValueRef) ABRecordCopyValue(ref, kABPersonPhoneProperty);

    NSString *phone=@"";

    NSString *homeNumber = @"";

    NSString *worknumber = @"";

    if(ABMultiValueGetCount(phones)>=1)
    {
        //int ph = [ABMultiValueCopyValueAtIndex(phones, 0) intValue];
        phone = (NSString *)ABMultiValueCopyValueAtIndex(phones,0);
    }
//  NSLog(@"%@",(NSString*)phone);  


    if(ABMultiValueGetCount(phones)>=2)
    {
        homeNumber = (NSString *)ABMultiValueCopyValueAtIndex(phones,1);
    }

    if(ABMultiValueGetCount(phones)>=3)
    {
        worknumber = (NSString *)ABMultiValueCopyValueAtIndex(phones,2);
    }


    NSMutableArray *arrayContacts = [[NSMutableArray alloc] init ];


    /////////////////////////////          insert into array               ////////////////////////////

    arrayContacts = [CoreDataAPIMethods getObjectsFromContext:@"AllContactData" :@"Index" :NO :self.managedObjectContext];

    ////////////////////////////         insert Index         ///////////////////////////////
    int NewEntryID;

    if ([arrayContacts count] > 0) 
    {
        AllContactData * Contacdata = [arrayContacts objectAtIndex:0];

        NewEntryID = [Contacdata.Index intValue] +1;

    }
    else 
    {
        NewEntryID = 1;
    }

    NSString *capitalisedSentence = 
    [(NSString *)firstName stringByReplacingCharactersInRange:NSMakeRange(0,1)  
                                        withString:[[(NSString *)firstName  substringToIndex:1] capitalizedString]];

    AllContactData *Contactitem=(AllContactData *)[NSEntityDescription insertNewObjectForEntityForName:@"AllContactData" inManagedObjectContext:self.managedObjectContext];

//      NSLog(@"%@",capitalisedSentence);

    Contactitem.Name = capitalisedSentence;

    Contactitem.LastName = (NSString*)lastName;

    Contactitem.NickName = (NSString*)nickName;

    Contactitem.MiddleName = (NSString*)middleName;

    Contactitem.Email=(NSString*)emailID;

    phone = [phone stringByReplacingOccurrencesOfString:@"(" withString:@""];

    phone = [phone stringByReplacingOccurrencesOfString:@")" withString:@""];

    phone = [phone stringByReplacingOccurrencesOfString:@"+" withString:@""];

    phone = [phone stringByReplacingOccurrencesOfString:@" " withString:@""];

    phone = [phone stringByReplacingOccurrencesOfString:@"-" withString:@""];

    NSLog(@"The Replaced String is : %@", phone);

    Contactitem.PhoneNumber=(NSString*)phone;

    Contactitem.HomeNumber=(NSString*)homeNumber;

    Contactitem.WorkNumber=(NSString*)worknumber;

    Contactitem.Index = [NSNumber numberWithInt:NewEntryID];

    Contactitem.Image = data;

//      NSLog(@"Image in databse  is : %@",(NSData *)ContactImage);

    if(firstName!=nil)
    {
        CFRelease(firstName);
    }
    CFRelease(ref);

}
CFRelease(allPeople);


    /////////////////////////////         save entries              ////////////////////////////

NSError *error;
if (![managedObjectContext save:&error]) {
    // Handle the error...
}


}

Upvotes: 0

Minakshi
Minakshi

Reputation: 1433

I am not able to give you the whole code. Following bare the steps : 1. On click to button go to new view which must have tableview 2. In that view get the address book

To get the addressbook

  1. Add these frameworks: AddressBook, AddressBookUI

  2. import them in your view

And for getting the contacts from addressbook refer the following tutorial

http://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Chapters/QuickStart.html#//apple_ref/doc/uid/TP40007744-CH2-SW1

I know this is not the full solution for your question but this link will help you to get the remaining task...Good luck

Upvotes: 0

Related Questions