Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何创建通讯簿和联系人_Ios_Addressbook - Fatal编程技术网

Ios 如何创建通讯簿和联系人

Ios 如何创建通讯簿和联系人,ios,addressbook,Ios,Addressbook,我已经为通讯录和显示联系人的联系人创建了一个代码。它工作正常,但突然出现运行时错误。因为我是ios新手。我找不到错误。堆栈溢出中的任何人都可以告诉我错误 - (void)viewDidLoad { [super viewDidLoad]; self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]]; self.view.backgroundColor = [UIColor yell

我已经为通讯录和显示联系人的联系人创建了一个代码。它工作正常,但突然出现运行时错误。因为我是ios新手。我找不到错误。堆栈溢出中的任何人都可以告诉我错误

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

}
-(void)viewDidLoad
{
[超级视图下载];
self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
self.view.backgroundColor=[UIColor yellowColor];
self.navigationItem.leftBarButtonItem=[[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarbuttonSystemCancel目标:自我操作:@selector(gotohomepage:)]autorelease];
ABPeoplePickerNavigationController*picker=[[ABPeoplePickerNavigationController alloc]init];
[[picker navigationBar]设置样式:UIBarStyleBlack];
picker.peoplePickerDelegate=self;
//仅显示一个人的电话、电子邮件和生日
NSArray*displayedItems=[NSArray arrayWithObjects:[NSNumber numberWithInt:KabbersonPhoneProperty],nil];
picker.displayedProperties=displayedItems;
[自我呈现ModalviewController:picker动画:是];
[选择器释放];
}
-(iAction)转到主页:(id)发件人
{
[自我解除视图控制器激活:是完成:无];
}
-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController*)peoplePickerNavigationController在选择person:(ABRecordRef)person后应继续
{
ABAddressBookRef addressBook=ABAddressBookCreate();
int i;
NSString*strName=@;
NSString*公司=@;
NSString*地址=@;
NSString*郊区=@;
NSString*postalcode=@”;
NSString*状态=@“”;
NSString*国家=@;
NSString*mobile=@;
NSString*phone=@;
NSString*emailid=@;
strName=(NSString*)ABRecordCopyCompositeName((ABRecordRef)person);
CFStringRef name=ABRecordCopyCompositeName((ABRecordRef)个人);
公司=(NSString*)ABRECORDCYVALUE((ABRecordRef)个人,KabbersonOrganizationProperty);
NSArray*allPeople=(NSArray*)ABAddressBookCopyPeopleWithName(addressBook,name);
(姓名);
对于(i=0;i<[所有人计数];i++)
{
ABRecordRef记录=[所有人反对索引:i];
ABMutableMultiValueRef multiValue=ABRecordCopyValue(记录,KabPersonalAddressProperty);

对于(CFIndex i=0;i错误在于,因为在IOS6上,苹果添加了一些功能,没有人有权在未经所有者许可的情况下获取内容。

在IOS6上,苹果引入了新的隐私控制,用户可以控制每个应用程序对联系人和日历的访问。因此,在代码方面,您需要添加一些方式来请求权限。在在5号或之前,我们随时可以打电话

ABAddressBookRef addressBook = ABAddressBookCreate();
要获取地址簿没有任何问题,但在iOS6中,如果您没有权限,此调用将只返回空指针。这就是为什么我们需要更改方法以获取ABAddressBookRef

这种类型的错误通常在未授予访问通讯簿的权限时出现。在这种情况下,请添加此代码

__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        accessGranted = granted;
        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    dispatch_release(sema);   
}
else { // we're on iOS 5 or older
    accessGranted = YES;
}

if (accessGranted) {
    // Do whatever you want here.
}
礼节:-


如果这不能解决您的问题,请将我ping回来。

将main中的错误作为Thread1:SIGABRT