5

我想在通讯录中创建一个新联系人。当您想要存储更多 URL(某些社交网络的网址)时,就会出现问题。我的代码在 iOS6 的模拟器中完美运行。但在 iOS6 的真实 iPhone 中,存储了除 urls 之外的所有值。我一直在寻找几天,但找不到解决方案,如果有人能提供帮助,我将不胜感激。

我的代码:

-(void) addContactToAddressBook:(ABAddressBookRef) iPhoneAddressBook 
{

    CFErrorRef error = NULL;

    ABRecordRef newPerson = ABPersonCreate();

    //Name and phone number
    ABRecordSetValue(newPerson, kABPersonFirstNameProperty, (__bridge CFStringRef)_nameField.text, &error);
    ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFStringRef)_phoneField.text, kABPersonPhoneMainLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);

    //Email value
    ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef)_emailField.text, kABWorkLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonEmailProperty, emailMultiValue, nil);
    CFRelease(emailMultiValue);

    //URL values
    ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_FacebookField.text, (CFStringRef)@"Facebook", NULL);
    ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_twitterField.text, (CFStringRef)@"Twitter", NULL);
    ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_linkedinField.text, (CFStringRef)@"Linkedin", NULL);
    ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_googleField.text, (CFStringRef)@"Google+", NULL);
    ABRecordSetValue(newPerson, kABPersonURLProperty, urlMultiValue, nil);
    CFRelease(urlMultiValue);

    ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);

    ABAddressBookSave(iPhoneAddressBook, &error);
    if (error != NULL)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Contact not saved" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
        [alert show];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Contact saved" message:@"Your contact was successfully saved" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
}

谢谢!

4

1 回答 1

0

根据苹果网站上的文档(向下滚动到页面中间的隐私),必须先授予对地址簿的访问权限,然后才能以编程方式访问它。这就是我最终要做的。

-(void)requestPermission
{
        ABAddressBookRef iPhoneAddressBook = ABAddressBookCreateWithOptions(NULL, NULL);

        if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
            ABAddressBookRequestAccessWithCompletion(iPhoneAddressBook, ^(bool granted, CFErrorRef error) {
                if (granted) {
                    // First time access has been granted, add the contact
                    [self addContactToAddressBook:iPhoneAddressBook];
                } else {
                    // User denied access
                    // Display an alert telling user the contact could not be added
                }
            });
        }
        else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
            // The user has previously given access, add the contact
            [self addContactToAddressBook:iPhoneAddressBook];
        }
        else {
            // The user has previously denied access
            // Send an alert telling user to change privacy setting in settings app
        }

    }

-(void)addContactToAddressBook:(ABAddressBookRef)iPhoneAddressBook
{
    CFErrorRef error = NULL;
    ABRecordRef newPerson = ABPersonCreate();

    //Name and phone number
    ABRecordSetValue(newPerson, kABPersonFirstNameProperty, (__bridge CFStringRef)_nameField.text, &error);
    ABMutableMultiValueRef multiPhone = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(multiPhone, (__bridge CFStringRef)_phoneField.text, kABPersonPhoneMainLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);
    CFRelease(multiPhone);

    //Email value
    ABMutableMultiValueRef emailMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(emailMultiValue, (__bridge CFStringRef)_emailField.text, kABWorkLabel, NULL);
    ABRecordSetValue(newPerson, kABPersonEmailProperty, emailMultiValue, nil);
    CFRelease(emailMultiValue);

    //URL values
    ABMutableMultiValueRef urlMultiValue = ABMultiValueCreateMutable(kABMultiStringPropertyType);
    ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_FacebookField.text, (CFStringRef)@"Facebook", NULL);
    ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_twitterField.text, (CFStringRef)@"Twitter", NULL);
    ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_linkedinField.text, (CFStringRef)@"Linkedin", NULL);
    ABMultiValueAddValueAndLabel(urlMultiValue, (__bridge CFStringRef)_googleField.text, (CFStringRef)@"Google+", NULL);
    ABRecordSetValue(newPerson, kABPersonURLProperty, urlMultiValue, nil);
    CFRelease(urlMultiValue);

    // ...
    // Set other properties
    // ...
    ABAddressBookAddRecord(iPhoneAddressBook, newPerson, &error);

    ABAddressBookSave(iPhoneAddressBook, &error);
    CFRelease(newPerson);
    CFRelease(iPhoneAddressBook);
    if (error != NULL)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Contact not saved" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
        [alert show];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Contact saved" message:@"Your contact was successfully saved" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];
    }
}
于 2015-03-31T06:22:21.357 回答