How to display a person record just after saving his data to iPhone Address Book?

Posted by camelCase on Stack Overflow See other posts from Stack Overflow or by camelCase
Published on 2010-09-18T21:31:24Z Indexed on 2010/12/25 2:54 UTC
Read the original article Hit count: 323

Filed under:
|
|
|

this is my code and it works flawless, where my_value is a string with separator ','. everythign works fin but i'd like to display the person record from the address book after i saved it, so in the function

  if(isSaved) {

        // **** code here ***
    }

here the complete function

- (void) addToAgenda: (NSString*) my_value{ 

    //NSArray *strings = [my_value componentsSeparatedByString: @","];
    NSArray *dati=[[NSArray alloc] initWithArray:[my_value componentsSeparatedByString:@","]];  
    NSString *userwebsite = [dati objectAtIndex:0];
    NSString *fname = [dati objectAtIndex:1];
    NSString *lname = [dati objectAtIndex:2];
    NSString *useremail = [dati objectAtIndex:3];;
    NSString *usermobile = [dati objectAtIndex:4];
    NSString *usercompany = @"xxx";



    ABRecordRef aRecord = ABPersonCreate(); 
    CFErrorRef  anError = NULL;

    // fisrst name
    ABRecordSetValue(aRecord, kABPersonFirstNameProperty, fname, &anError);

    // last name
    ABRecordSetValue(aRecord, kABPersonLastNameProperty, lname, &anError);

    // Phone Number.
    ABMutableMultiValueRef multi = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multi, (CFStringRef)usermobile, kABWorkLabel, NULL);
    ABRecordSetValue(aRecord, kABPersonPhoneProperty, multi, &anError);
    CFRelease(multi);

    // Company
    ABRecordSetValue(aRecord, kABPersonDepartmentProperty, usercompany, &anError);

    // email
    NSLog(@"%@", useremail);
    ABMutableMultiValueRef multiemail = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiemail, (CFStringRef)useremail, kABWorkLabel, NULL);
    ABRecordSetValue(aRecord, kABPersonEmailProperty, multiemail, &anError);
    CFRelease(multiemail);

    // website
    NSLog(@"%@", userwebsite);
    ABMutableMultiValueRef multiweb = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiweb, (CFStringRef)userwebsite, kABWorkLabel, NULL);
    ABRecordSetValue(aRecord, kABPersonURLProperty, multiweb, &anError);
    CFRelease(multiemail);


    if (anError != NULL)
        NSLog(@"error while creating..");

    CFStringRef personname, personlname, personcompind, personemail, personwebsite, personcontact;

    personname = ABRecordCopyValue(aRecord, kABPersonFirstNameProperty); 
    personlname = ABRecordCopyValue(aRecord, kABPersonLastNameProperty); 
    personcompind = ABRecordCopyValue(aRecord, kABPersonDepartmentProperty); 
    personemail = ABRecordCopyValue(aRecord, kABPersonEmailProperty);
    personwebsite = ABRecordCopyValue(aRecord, kABPersonURLProperty);
    personcontact  = ABRecordCopyValue(aRecord, kABPersonPhoneProperty); 

    ABAddressBookRef addressBook; 
    CFErrorRef error = NULL; 
    addressBook = ABAddressBookCreate(); 

    BOOL isAdded = ABAddressBookAddRecord (addressBook, aRecord, &error);

    if(isAdded){

        NSLog(@"added..");
    }
    if (error != NULL) {
        NSLog(@"ABAddressBookAddRecord %@", error);
    } 
    error = NULL;

    BOOL isSaved = ABAddressBookSave (addressBook, &error);

    if(isSaved) {

        // **** code here ***
    }

    if (error != NULL) {

        NSLog(@"ABAddressBookSave %@", error);
        UIAlertView *alertOnChoose = [[UIAlertView alloc] initWithTitle:@"Unable to save this time" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil];
        [alertOnChoose show];
        [alertOnChoose release];
    } 

    CFRelease(aRecord); 
    CFRelease(personname);
    CFRelease(personlname);
    CFRelease(personcompind);
    CFRelease(personcontact);
    CFRelease(personemail);
    CFRelease(personwebsite);  
    CFRelease(addressBook); 


}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about cocoa-touch