开发者

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

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(@"%@"开发者_JS百科, 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); 
}


You can use something like ABPersonViewController to display aRecord. Since it is a subclass of UIViewController, you can show it just like any other view controller, such as pushing it onto your navigation controller stack or presenting it modally.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜