【问题标题】:Custom addressbook ABPeoplePickerViewController help please?自定义地址簿 ABPeoplePickerViewController 请帮忙?
【发布时间】:2012-12-11 09:38:03
【问题描述】:

我需要有关 iPhone 的自定义地址簿 (ABPeoplePickerViewController) 的帮助吗? 我想要一个包含我所有联系人的数组,只将他们的姓名和号码拉到表格视图的单元格中进行显示。选择一些联系人,打开消息并向他们发送带有自定义消息的文本/短信。

WhatsApp messenger 是一个很棒的例子,如果你去设置,告诉朋友,然后消息.. 我想要那个样子!

它必须是自定义的,因为我还想要下面的发送和取消按钮,以及 cell.textLabel 中的名称及其在 subtitle 样式表视图中的 cell.detailTextLabel 中的编号。

那么我如何从通讯录中获取他们的详细信息并放入我的数组(contactsName、contactsNumber)?提前致谢!这是我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [contactsName objectAtIndex:indexPath.row];
    cell.detailTextLabel.text = [contactsNumber objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [self dismissViewControllerAnimated:YES completion:^{ NSLog(@"Message controller has been canceled"); }];
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    NSString *name = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
    NSString *number = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSLog(@"Name: %@ Number: %@", name, number);

    return NO;
}

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    return NO;
}

- (void)openSMScontroller {
    MFMessageComposeViewController *smsView = [[MFMessageComposeViewController alloc] init];
    smsView.messageComposeDelegate = self;

    smsView.recipients = [NSArray arrayWithArray:contactsNumber];
    smsView.body = @"Check out this awesome app!";

    [self presentModalViewController:smsView animated:YES];
}

【问题讨论】:

    标签: iphone objective-c ios uitableview abaddressbook


    【解决方案1】:

    你可以通过下面的代码从通讯录中得到你想要的东西我的朋友。!!!!!

    编码愉快!!!!!!

    - (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
     ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);
       ABMultiValueRef addressProperty = ABRecordCopyValue(person, kABPersonAddressProperty);
     NSArray *address = (NSArray *)ABMultiValueCopyArrayOfAllValues(addressProperty);
    if([address count] > 0)
    {
        for (NSDictionary *addressDict in address)
        {
            countrytf.text = [addressDict objectForKey:@"Country"];
            streetaddresstf.text = [addressDict objectForKey:@"Street"];
            citynametf.text = [addressDict objectForKey:@"City"];
            statenametf.text = [addressDict objectForKey:@"State"];
            zipcodetf.text = [addressDict objectForKey:@"ZIP"];
    
        }
        CFRelease(addressProperty);
    }
    }
    

    【讨论】:

    • 感谢您的回答,稍后再试,如果效果不佳,请标记您的答案,谢谢.. :) @NiravPatel
    • 什么是“countrytf”等?我不想要地址,只想要数字和姓名,但如果我删除那些地址行,我会收到 phoneNumbersaddressProperty 的警告?你能解释一下或者给我另一个代码吗? :
    • countrytf,streetaddresstf 等只是我在我的应用程序中使用的文本字段,您可以使用 NSString 代替我的朋友...为了获取电话号码,您可以按照 ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhone 属性); NSString* phone = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0); nslog(@"phone:%@",phone);
    • 您能告诉我如何将选定的联系人(姓名、号码)放入 1 个字典,然后将文本/短信发送给选定的联系人吗?请给个方向好吗?
    【解决方案2】:

    要获取电话号码...

    ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty); 
     NSString*    phone = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, 0); 
    
     nslog(@"phone:%@",phone); 
    

    让我知道它是否有效!!!!!!请如果它是正确的然后奖励它,我知道它是正确的..

    快乐编码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-24
      • 2011-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      相关资源
      最近更新 更多