- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想在我的联系人 页面中添加地址簿,并且我想以编程方式即而不使用nib 文件
。任何人都可以建议我一个很好的教程或示例代码。我使用了 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 文件中添加所有的 Delegate 和 Datasource 方法
<ABPeoplePickerNavigationControllerDelegate,ABPersonViewControllerDelegate,ABNewPersonViewControllerDelegate,ABUnknownPersonViewControllerDelegate>
创建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
的Delegate和DataSource方法
#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;
}
谢谢 :)
关于iphone - 在联系人页面添加通讯录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14729959/
您好,我是个菜鸟,如果您对此有任何帮助,我将不胜感激。我正在尝试使用用户保存在他们地址簿中的数据,之后我想让他们选择使用该地址进行递送。 这是我的地址 Controller class Address
我目前正在为我的公司开发一个网络应用程序。在此应用程序中,有一个表单,其中的输入字段包含有关联系人的信息。我想创建一个按钮,单击即可在地址簿中创建联系人。 是否可以使用 JavaScript 或其他网
在隐藏通讯录界面之前,是否可以一次选择多个联系人?还是每次地址簿界面出现时实际上只需要选择一个联系人? 最佳答案 是的,Whatsapp做到了。 如果您需要一个示例,请参见以下示例: MultiCon
我正在尝试获取 Android 中联系人的所有电话号码。我的代码如下所示: ContentResolver cr = context.getContentResolver(); String[] pr
我正在为域中的所有用户构建一个表。我让表格正常工作,并在其中为我的分页添加了一些代码,但现在我不断收到错误... //connection $conn = new mysqli($servername
我正在开发一个可以访问 iPhone 通讯录的应用程序。模拟器仅包含几个示例条目。我想找到一个包含数千个条目的示例地址簿字符集(英语、中文、阿拉伯语等)来全面测试我的应用程序。自己填写地址簿将花费永远
是否有任何 android API 可以提取 vcard 3.0 格式的联系人?我遇到了一个开源 vcard 项目 android-vcard 但在用法示例中提到了以下注释。 重要提示:如果您正在为
我的应用程序能够在应用程序的不同实例之间共享其内容,即有一个按钮可以让我将文档附加到电子邮件并将其发送给应用程序的另一个用户。当他们收到此文件时,它会在他们的应用程序实例中打开。这一切正常。 但是,在
我正在开发 iPhone 联系人管理应用程序。一位用户报告说他的所有联系人都是重复的。当他选择特定来源时,联系人不会重复。显然,这是因为他的地址簿同步了两次:一次是与他 PC 上的 iTunes 同步
我有两个继承自 UIViewController 的 Objective-C 类,我正在尝试一种不同的方法来学习如何与 iPhone 的地址簿进行交互。 example Apple 假设一切都在一个类
我想添加在 iOS 通讯录中保存联系人(电话号码、姓名、地址和电子邮件)的功能,方法是单击我的应用程序中的一个按钮!我该怎么做? 最佳答案 非常简单,只需按照以下步骤操作, 首先,您需要导入以下框架,
我正在使用新的 API 在我的手机上插入/更新联系人,我正在使用这种方法来更新它们.. String selectArgs1 = Data.CONTACT_ID + "=? AND " + Data.
我目前正在开发 Contact Importer 网络应用程序(使用 PHP),这样我就可以从 Gmail、Yahoo 等网站的用户帐户中获取电子邮件地址,并将它们用于我自己的邪恶目的。开个玩笑,我的
我与 Google 通讯录合作,一切顺利。但一个小时后,我需要手动刷新授予的访问权限。据此SO question我应该能够通过这个调用更新我的 refresh_token : refresh_toke
我正在使用以下代码获取 google 联系人姓名和电话号码。授权页面本身没有正常显示错误,如“您请求的页面无效”。 :( 请帮我解决这个问题... ` google.load("gdata",
正在尝试使用 google contacts api,如果我使用的话 https://www.google.com/m8/feeds/contacts/default/full 我有 25 个联系人,
由于 Google 停止了对旧版 Auth 的支持,现在我们必须使用 oAuth 2,我们简单的桌面应用程序无法再从我的 Google 帐户读取联系人。 很好 - 我明白这一点,但是这个新的 oAut
我想以编程方式将联系人直接保存到 iOS 设备的地址簿。 我该怎么做? 最佳答案 这是一个小例子: CFErrorRef error = NULL; NSLog(@"%@", [self descri
我正在尝试使用 Javascript 提取我在 Google 通讯录 ( https://contacts.google.com ) 上的联系人。为此,我需要将联系人面板滚动到底部,以便获取所有页面源
我目前正在开发一个 VoIP 应用程序,但我真的很受地址簿困扰。 由于自定义设计, native 地址簿不适合我的应用程序。此外,我想添加一些 native 地址簿中不存在的额外数据。但这会导致一些问
我是一名优秀的程序员,十分优秀!