如何在ios中向地址簿添加联系人 [英] how to add contacts to addressbook in ios

查看:82
本文介绍了如何在ios中向地址簿添加联系人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为地址簿和联系人创建了一个代码,用于显示联系人。它工作正常,但突然出现运行时错误。由于我是ios的新手,我无法找到错误,Stack Overflow中的任何一个都可以告诉错误。

I have created a code for Addressbook and contacts where display the contacts.It was working properly but suddenly getting Run time error. As I''m new to ios, I''m not able to find the error can any one in the Stack Overflow tell the error.

- (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;
// Display only a person's phone, email, and birthdate
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);

        // 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;

}

推荐答案

!< Home>!
!<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 stringWith格式: @ %@,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< ABM​​ultiValueGetCount(phones); i ++)
{
mobileLabel =(NSString *)ABMultiValueCopyLabelAtIndex(phones,i);
if ([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
{
mobile =(NSString *)ABMultiValueCopyValueAtIndex(phone,一世);
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);

// 检查工作电子邮件标签
< span class =code-keyword> if (CFStringCompare(label,kABWorkLabel, 0 )== 0
{
emailid =(NSString *) value ;
NSLog( @ self.emailID%@,emailid);
}
else if (CFStringCompare(label,kABHomeLabel, 0 )== 0
{
emailid =(NSString *);
NSLog( @ self.emailID%@,emailid);
}

CFRelease(label);
CFRelease( value );
}
}
CFRelease(multi);
CFRelease(电话);
CFRelease(addressBook);

[self dismissModalViewControllerAnimated:YES];

return NO;

}
_"]) { 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; }


这篇关于如何在ios中向地址簿添加联系人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆