在联系页面添加地址簿

2024-01-11

我想在我的地址簿中添加contact页面,我想以编程方式执行此操作i.e不使用nib files。谁能给我推荐一个不错的教程或示例代码。我已经使用了 iPatel 给出的答案的代码,当我运行时它抛出异常并且应用程序正在终止。 感谢致敬。

这是编辑后的代码。

#import "ContactInfoViewController.h"

@interface ContactInfoViewController ()

@end

@implementation ContactInfoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
self.view.backgroundColor = [UIColor yellowColor];
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(gotohomepage:)]autorelease];
ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
[[picker navigationBar] setBarStyle:UIBarStyleBlack];
picker.peoplePickerDelegate = self;
NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],nil];


picker.displayedProperties = displayedItems;
[self presentModalViewController:picker animated:YES];
[picker release];


}
- (IBAction)gotohomepage:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{

ABAddressBookRef addressBook = ABAddressBookCreate();
int i;
NSString *strName = @"";
NSString* company = @"";
NSString *address = @"";
NSString *suburb = @"";
NSString *postalcode = @"";
NSString *state = @"";
NSString *country = @"";
NSString *mobile = @"";
NSString *phone = @"";
NSString *emailid = @"";
strName = (NSString *)ABRecordCopyCompositeName((ABRecordRef) person);
CFStringRef name = ABRecordCopyCompositeName((ABRecordRef) person);
company  = (NSString *)ABRecordCopyValue((ABRecordRef) person, kABPersonOrganizationProperty);
NSArray*  allPeople = (NSArray *)ABAddressBookCopyPeopleWithName(addressBook,name);
CFRelease(name);

for (i = 0; i < [allPeople count]; i++)
{
    ABRecordRef record = [allPeople objectAtIndex:i];

    ABMutableMultiValueRef multiValue = ABRecordCopyValue(record, kABPersonAddressProperty);
    for(CFIndex i=0; i<ABMultiValueGetCount(multiValue); i++)
    {
        NSString* HomeLabel = (NSString*)ABMultiValueCopyLabelAtIndex(multiValue, i);
        if([HomeLabel isEqualToString:@"_$!<Home>!$_"])
        {
            CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(multiValue, i);
            address = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressStreetKey)];
            suburb = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressCityKey)];
            postalcode = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressZIPKey)];
            state = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressStateKey)];
            country = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict,  kABPersonAddressCountryKey)];
            CFRelease(dict);
        }
        CFRelease(HomeLabel);
    }
    CFRelease(multiValue);
}
CFRelease(allPeople);


ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
NSString* mobileLabel = nil;
for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++)
{
    mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
    if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
    {
        mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
        NSLog(@"phone   %@",mobile);
    }
    else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
    {
        phone = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
        NSLog(@"phone   %@",phone);

        CFRelease(mobileLabel);
        break ;
    }
    CFRelease(mobileLabel);

}
CFStringRef value, label;
ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty);
CFIndex count = ABMultiValueGetCount(multi);
if (count == 1)
{
    value = ABMultiValueCopyValueAtIndex(multi, 0);
    emailid = (NSString*) value;
    NSLog(@"self.emailID   %@",emailid);
    CFRelease(value);
}
else
{
    for (CFIndex i = 0; i < count; i++)
    {
        label = ABMultiValueCopyLabelAtIndex(multi, i);
        value = ABMultiValueCopyValueAtIndex(multi, i);
        if (CFStringCompare(label, kABWorkLabel, 0) == 0)
        {
            emailid = (NSString*) value;
            NSLog(@"self.emailID   %@",emailid);
        }
        else if(CFStringCompare(label, kABHomeLabel, 0) == 0)
        {
            emailid = (NSString*) value;
            NSLog(@"self.emailID   %@",emailid);
        }
        CFRelease(label);
        CFRelease(value);
    }
}
CFRelease(multi);
 CFRelease(phones);
CFRelease(addressBook);
[self dismissModalViewControllerAnimated:YES];
return NO;
}
#pragma mark - ABPeopelPickerNavigationController Delegate and DataSource Methods
- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
[self dismissModalViewControllerAnimated:YES];
}
- (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownCardViewController didResolveToPerson:(ABRecordRef)person
{
}
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person
{
}
- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
return YES;
}
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController  *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier;
{
return YES;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end

EDIT :

首先在类中添加所有委托和数据源方法.h file

<ABPeoplePickerNavigationControllerDelegate,ABPersonViewControllerDelegate,ABNewPersonViewControllerDelegate,ABUnknownPersonViewControllerDelegate>

Create ABPeoplePickerNavigationController

ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
        [[picker navigationBar] setBarStyle:UIBarStyleBlack];
        picker.peoplePickerDelegate = self;
        // Display only a person's phone, email, and birthdate
        NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty],nil];


        picker.displayedProperties = displayedItems;
        // Show the picker
        [self presentModalViewController:picker animated:YES];
        [picker release];

添加以下委托和数据源方法ABPeopelPickerNavigationController

#pragma mark - ABPeopelPickerNavigationController Delegate and DataSource Methods

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [self dismissModalViewControllerAnimated:YES];
}


- (void)unknownPersonViewController:(ABUnknownPersonViewController *)unknownCardViewController didResolveToPerson:(ABRecordRef)person
{
}

- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person
{
}

- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
    return YES;
}

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

请尝试下面的代码从电话簿中获取人员的所有信息

- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    ABAddressBookRef addressBook = ABAddressBookCreate();

    int i;
    NSString *strName = @"";
    NSString* company = @"";
    NSString *address = @"";
    NSString *suburb = @"";
    NSString *postalcode = @"";
    NSString *state = @"";
    NSString *country = @"";
    NSString *mobile = @"";
    NSString *phone = @"";
    NSString *emailid = @"";


    strName = (NSString *)ABRecordCopyCompositeName((ABRecordRef) person);
    CFStringRef name = ABRecordCopyCompositeName((ABRecordRef) person);
    company  = (NSString *)ABRecordCopyValue((ABRecordRef) person, kABPersonOrganizationProperty);

    NSArray*  allPeople = (NSArray *)ABAddressBookCopyPeopleWithName(addressBook,name);
    CFRelease(name);

    for (i = 0; i < [allPeople count]; i++)
    {
        ABRecordRef record = [allPeople objectAtIndex:i];

        ABMutableMultiValueRef multiValue = ABRecordCopyValue(record, kABPersonAddressProperty);
        for(CFIndex i=0; i<ABMultiValueGetCount(multiValue); i++)
        {
            NSString* HomeLabel = (NSString*)ABMultiValueCopyLabelAtIndex(multiValue, i);
            if([HomeLabel isEqualToString:@"_$!<Home>!$_"])
            {
                CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(multiValue, i);
                address = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressStreetKey)];
                suburb = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressCityKey)];
                postalcode = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressZIPKey)];
                state = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressStateKey)];
                country = [NSString stringWithFormat:@"%@", CFDictionaryGetValue(dict, kABPersonAddressCountryKey)];

                CFRelease(dict);
            }
            CFRelease(HomeLabel);
        }
        CFRelease(multiValue);
    }
    CFRelease(allPeople);


    ABMultiValueRef phones =(NSString*)ABRecordCopyValue(person, kABPersonPhoneProperty);
    NSString* mobileLabel = nil;
    for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++)
    {
        mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
        if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
        {
            mobile = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
            NSLog(@"phone   %@",mobile);
        }
        else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
        {
            phone = (NSString*)ABMultiValueCopyValueAtIndex(phones, i);
            NSLog(@"phone   %@",phone);

            CFRelease(mobileLabel);
            break ;
        }
        CFRelease(mobileLabel);

    }
    CFStringRef value, label;
    ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty);
    CFIndex count = ABMultiValueGetCount(multi);
    if (count == 1)
    {
        value = ABMultiValueCopyValueAtIndex(multi, 0);
        emailid = (NSString*) value;
        NSLog(@"self.emailID   %@",emailid);
        CFRelease(value);
    }
    else
    {
        for (CFIndex i = 0; i < count; i++)
        {
            label = ABMultiValueCopyLabelAtIndex(multi, i);
            value = ABMultiValueCopyValueAtIndex(multi, i);

            // check for Work e-mail label
            if (CFStringCompare(label, kABWorkLabel, 0) == 0)
            {
                emailid = (NSString*) value;
                NSLog(@"self.emailID   %@",emailid);
            }
            else if(CFStringCompare(label, kABHomeLabel, 0) == 0)
            {
                emailid = (NSString*) value;
                NSLog(@"self.emailID   %@",emailid);
            }

            CFRelease(label);
            CFRelease(value);
        }
    }
    CFRelease(multi);

        }


    CFRelease(phones);
    CFRelease(addressBook);
    [self dismissModalViewControllerAnimated:YES];

    return NO;

}

欲了解更多信息,请阅读this http://developer.apple.com/library/ios/#documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Chapters/QuickStart.html and this http://blog.slaunchaman.com/2009/01/21/cocoa-touch-tutorial-extract-address-book-address-values-on-iphone-os/教程。

谢谢 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在联系页面添加地址簿 的相关文章

  • 将 NSDate 转换为 SWIFT 中具有特定时区的字符串

    在我的核心数据库中 我有一个带有 NSDate 属性的 新闻 实体 我的应用程序遍布全球 新闻已发布2015 09 04 22 15 54 0000法国时间 GMT 2 为了保存日期 我将其转换为 UTC 格式 let mydateForm
  • 我以前没见过的 CGRect 语法

    我在一些示例代码中看到了下面的语法 但不确定我是否理解它 CGRect imageRect CGRect size baseImage size 这只是初始化的一种简写方式吗CGRect相当于 CGRect imageRect CGRect
  • UINavigationControllerDelegate的didShowViewController方法被调用了两次

    class ViewController UIViewController UINavigationControllerDelegate override func viewDidLoad super viewDidLoad navigat
  • ios 8 opengl es 1.1 已停产?

    我们即将在 iOS 应用商店上推出一款游戏 最近我们发现它无法在 iOS 8 上运行 游戏加载到黑屏 但其他一切似乎都可以运行 可以听到音乐 对触摸屏有反应 但显示屏上没有任何反应 我们的引擎相当旧并且使用 OpenGL ES 1 1 我现
  • 核心蓝牙在后台进行广告和扫描

    我一直在尝试设置一个应用程序 使设备既扫描外围设备又作为外围设备进行广告 目标是当两个设备通过蓝牙发现彼此靠近时在后台被唤醒 从 Apple 文档来看 您似乎应该能够在后台运行 BLE 启用蓝牙中心和蓝牙外设后台模式 并且当一台设备位于前台
  • 删除 NSMutablearray 中的最后一个对象[关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 为什么要删
  • 如何在 Monotouch 中对 UIImageView 进行运动模糊效果?

    在 MonoTouch 中进行实时运动模糊的方法是什么 当滚动惯性图片库时 我需要在 UIImageView 上应用运动模糊效果 以强度和方向作为参数 就像在 Photoshop 中一样 我在 CocoaTouch 或 CoreAnimat
  • iPhone - 创建图像网格列表

    我正在开发一个应用程序 它从网站获取图像并使用网格视图向用户显示 例如Apple的照片应用程序 当您点击图像时 我会推送一个显示图像信息和其他内容的视图控制器 实现这个的更好方法是什么 我想到了自定义 UITableViewCell 我见过
  • 如何修复C风格的for语句?

    什么是正确的修复方法C 风格的 for 语句对于下面发布的代码 目前我正在交战 C 风格的 for 语句已弃用 并将在将来删除 斯威夫特的版本 var ifaddr UnsafeMutablePointer
  • CNContact 添加新的联系人问题

    我在通过以下方式添加联系人时遇到问题联系框架 我使用的是装有 iOS 12 1 2 的 iPhone 5s 设备 我添加联系人的代码如下 let saveRequest CNSaveRequest saveRequest add self
  • 使用prefersLargeTitles 和 UITableView 平滑滚动

    我在使用时遇到了滚动问题prefersLargeTitles并添加了UITableView 如果我设置prefersLargeTitles在导航控制器中 其根是UITableViewController一切都很好 导航大标题的滚动方式与我们
  • iOS UITableViewCell 配件在左侧?

    对于我的应用程序 我想要一些可以同时具有复选标记和详细信息披露按钮的单元格 也就是说 我希望它们看起来与 iOS 设置中的 Wi Fi 网络选择一模一样 左侧的复选标记 中间的内容 右侧的详细信息披露按钮 有没有正确的方法来做到这一点 或者
  • 找不到导航对象。您的组件是否位于导航器屏幕内?

    在我下面的代码中 当我使用 useNavigation 时 它会给出像我的问题一样的错误 如何使用useNavigation 请任何人都可以解决此错误 错误 找不到导航对象 您的组件是否位于导航器屏幕内 我从这里跟踪了代码https rnf
  • 使用 NSSet/NSMutableSet 来过滤对象?

    我已经看到一些有关使用 NSSet NSMutableSet 过滤对象的问题 答案 这些情况使用简单类型的对象 例如NSString or int 以下是用于过滤的代码示例NSString对象 NSSet smallArray NSSet
  • iphone:通过代码获取目标设置中的用户定义变量?

    我的项目有多个目标 每个目标都有自己的目标Class用于设置内容的文件 我想存储它Class目标设置中的名称 Info plist或目标的建筑物设置 这样我就可以根据此设置定义每个目标中需要使用哪个类 根据这个问题 https stacko
  • ios 8 核心数据崩溃

    保存时 CoreData 发生崩溃 2014 09 16 09 51 58 273 My app 2678 105246 Terminating app due to uncaught exception NSInvalidArgument
  • cordova-plugin-whitelist 适用于 Android,但不适用于 iOS (Phonegap Build)

    我正在开发一个用 Cordova 封装并使用 Phonegap Build 构建的 JavaScript 应用程序 我们包括cordova plugin whitelist来自我们构建中的 npm 并添加了
  • 删除后台 moc 中的对象然后在主 moc 中刷新它会导致 NSFetchedResultsController 更新崩溃

    我遇到了一个NSObjectInaccessibleException我无法理解 Terminating app due to uncaught exception NSObjectInaccessibleException reason
  • 在 UITableviewCell 高度动画的同时动画 CALayer 阴影

    我有一个 UITableView 我正在尝试使用它来展开和折叠beginUpdates and endUpdates方法并在发生时显示阴影 在我的自定义 UITableViewCell 中 我有一个图层 我为其创建阴影layoutSubvi
  • 应用程序图标未刷新

    我更改了新版本应用程序中的图标图像 并且我在设备中安装了旧版本应用程序 然后我安装了新版本 它在 iOS 5 中运行良好 但在 iOS 6 中 图标没有刷新 它仍然显示旧版本图标 徽标 如果没有安装旧版本应用程序 该设备在 iOS 5 和

随机推荐