0

I want to replicate add contact like screen iPhone has. But I don't want to add contact in default phonebook instead I want to use the details in my app. Is it possible to open default add new contact screen and get all the data? If yes then how? A simple code snippet will be very helpful. Here is an image of add contact screen to better understand my question

Add New Contact

Rahul Vyas
  • 28,260
  • 49
  • 182
  • 256

1 Answers1

1

You can try to add the contact to the address book, pull the data and then delete it from the address book. this is a fairly simple process.

I use this function to save all the person data to core data in my app. and then to delete the person from the addressBook.

  +(void)savePersonDetails:(Person*)person{

ABAddressBookRef addressBook = ABAddressBookCreate();
ABRecordRef ref = ABAddressBookGetPersonWithRecordID(addressBook,[person.ID intValue]);
ABMutableMultiValueRef multiPhones = ABRecordCopyValue(ref, kABPersonPhoneProperty);

for (CFIndex i = 0; i < ABMultiValueGetCount(multiPhones); i++) {
    NSString *phoneNumber  = (NSString*)ABMultiValueCopyValueAtIndex(multiPhones, i);
    CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(multiPhones, i);
    NSString *phoneNumberLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);
    CFRelease(locLabel);


    Phone *phone =(Phone*)[NSEntityDescription insertNewObjectForEntityForName:@"Phone" inManagedObjectContext:person.managedObjectContext];
    phone.number =  phoneNumber;
    phone.label = phoneNumberLabel;
    phone.person = person;
    [person addPhonesObject:phone];

    [person release];
    CFRelease(phoneNumber);
    CFRelease(phoneNumberLabel);

}

  CFRelease(multiPhones);   


   ABMutableMultiValueRef multiEmail = ABRecordCopyValue(ref, kABPersonEmailProperty);

for (CFIndex i = 0; i < ABMultiValueGetCount(multiEmail); i++) {
    NSString *mail = (NSString*)ABMultiValueCopyValueAtIndex(multiEmail, i);
    CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(multiEmail, i);
    NSString *mailLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);

    Mail *mailEntity =(Mail*)[NSEntityDescription insertNewObjectForEntityForName:@"Mail" inManagedObjectContext:person.managedObjectContext];
    mailEntity.mail = mail;
    mailEntity.label = mailLabel;
    mailEntity.person = person;
    [person addMailsObject:mailEntity];

    CFRelease(locLabel); 
    [mail release];
    [mailLabel release];
}
    CFRelease(multiEmail);


    ABMultiValueRef streets = ABRecordCopyValue(ref, kABPersonAddressProperty);
for (CFIndex j = 0; j<ABMultiValueGetCount(streets);j++){
    CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(streets, j);
    CFStringRef typeTmp = ABMultiValueCopyLabelAtIndex(streets, j);
    CFStringRef lbl = ABAddressBookCopyLocalizedLabel(typeTmp);
    NSString *street = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStreetKey) copy];
    NSString *city = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCityKey) copy];
    NSString *state = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressStateKey) copy];
    NSString *zip = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressZIPKey) copy];
    NSString *country = [(NSString *)CFDictionaryGetValue(dict, kABPersonAddressCountryKey) copy];





    Address *addressEntity =(Address*)[NSEntityDescription    insertNewObjectForEntityForName:@"Address"    inManagedObjectContext:person.managedObjectContext];
    addressEntity.label = (NSString*)lbl;
    addressEntity.street = street;
    addressEntity.city = city;
    addressEntity.state = state;
    addressEntity.zip = zip;
    addressEntity.country = country;


    [street release];
    [city release];
    [state release];
    [zip release];
    [country release];
    CFRelease(dict);
    CFRelease(lbl);
    CFRelease(typeTmp);
    addressEntity.person = person;
    [person addAddressesObject:addressEntity];
}
CFRelease(streets);



ABMutableMultiValueRef multiURL = ABRecordCopyValue(ref, kABPersonURLProperty);

for (CFIndex i = 0; i < ABMultiValueGetCount(multiURL); i++) {

    NSString *url = (NSString*)ABMultiValueCopyValueAtIndex(multiURL, i);
    CFStringRef locLabel = ABMultiValueCopyLabelAtIndex(multiPhones, i);
    NSString *urlLabel =(NSString*) ABAddressBookCopyLocalizedLabel(locLabel);

    Url *urlEntity =(Url*)[NSEntityDescription   insertNewObjectForEntityForName:@"Url" inManagedObjectContext:person.managedObjectContext];
    urlEntity.url = url;
    urlEntity.label = urlLabel;
    urlEntity.person = person;
    [person addUrlsObject:urlEntity];



    CFRelease(locLabel);
    [urlLabel release];
    [url release];
}
    CFRelease(multiURL);


ABAddressBookRemoveRecord(addressBook, ref, nil);
ABAddressBookSave(addressBook, nil);

CFRelease(addressBook);

if (![person.managedObjectContext save:&error]) {
    // Update to handle the error appropriately.
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    exit(-1);  // Fail
}
  }
shannoga
  • 19,649
  • 20
  • 104
  • 169
  • I also want to display the add new contact screen in my app.Could you please convert your code to simple insert-> Get -> storeIntoOwnDb -> deleteFromAddressBook? I am unable to understand that how do i manage multiple fields of phone and email in my own database? Could you please help regarding this way? Like what should be the table schema for contacts and all the related things? And when fetching how do I create data Modal for all the details? – Rahul Vyas Jun 06 '11 at 08:39
  • Hi Rahul. Well what you are asking is a liitle bit to much to answer in a furom. You will have to learn a little bit more on coreData. I really recommend you to do so as it will open a lot of dors for you. Meanwhile I can recommend you to check sensibleTableView. They have a great API to create your own form very simply and store the data in core data with minimum skills. Check it out in their website. – shannoga Jun 06 '11 at 11:05
  • I don't want to use core data I want to use sqlite – Rahul Vyas Jun 06 '11 at 14:33
  • Sorry but i don't know how to use sqlite. – shannoga Jun 08 '11 at 18:47