试图添加新的联系人到ios app抛出错误

时间:2014-04-05 19:24:52

标签: ios iphone contacts addressbook

我正在写我的第一个iPhone应用程序。我试图在我的代码中添加一个新联系人,当我这样做时,我收到以下错误:错误Domain = ABAddressBookErrorDomain Code = 1“操作无法完成。(ABAddressBookErrorDomain错误1。)”。

我搜索了一下,似乎应用程序可能无法访问联系人应用程序,但我认为我已经检查并编码,如果访问被拒绝请求访问。有人可以帮忙吗?

这是我的代码:

    CFErrorRef err = nil;
    ABAuthorizationStatus stat = ABAddressBookGetAuthorizationStatus();
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    ABRecordRef person = ABPersonCreate();
    ABMutableMultiValueRef addr = ABMultiValueCreateMutable(kABMultiStringPropertyType);

    if (addressBook == nil) {
        NSLog(@"error: %@", err);
    }
    else{
        if (stat==kABAuthorizationStatusDenied || stat==kABAuthorizationStatusRestricted) {
            NSLog(@"%@", @"no access.  Please change privacy settings under Privacy > Settings");
        }
        else{
            NSLog(@"Starting Contact Build...");
            ABRecordSetValue(person, kABPersonFirstNameProperty, @"Jimmy", &err);
            ABRecordSetValue(person, kABPersonLastNameProperty, @"Johns", &err);

            ABMultiValueAddValueAndLabel(addr, @"1-123-456-7890", kABPersonPhoneIPhoneLabel, NULL);

            ABMultiValueAddValueAndLabel(addr, @"jimmy@corpmail.com", kABHomeLabel, nil);
            ABRecordSetValue(person, kABPersonEmailProperty, addr, nil);

            ABAddressBookAddRecord(addressBook, person, &err);
            ABAddressBookSave(addressBook, &err);
            if (addr) CFRelease(addr);
            if (person) CFRelease(person);


            if (err != NULL)
            {

                NSLog(@"Some error... %@", err);

            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我遇到了没有访问权限的相同问题。我已经用下面链接的帮助解决了

答案由Kyle发布:https://stackoverflow.com/a/12648938/480415