在将数据保存到iPhone地址簿后如何显示人员记录?

时间:2021-09-02 20:05:28

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

这是我的代码,它完美无缺,其中my_value是一个带分隔符','的字符串。 everythign工作fin但我想在保存之后显示地址簿中的人员记录,所以在功能中

  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); 
}

1 个解决方案

#1


0  

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.

您可以使用类似ABPersonViewController的内容来显示aRecord。由于它是UIViewController的子类,因此您可以像任何其他视图控制器一样显示它,例如将其推送到导航控制器堆栈或以模态方式呈现它。

#1


0  

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.

您可以使用类似ABPersonViewController的内容来显示aRecord。由于它是UIViewController的子类,因此您可以像任何其他视图控制器一样显示它,例如将其推送到导航控制器堆栈或以模态方式呈现它。