无法使用AddressBook框架在addressBook中保存名称和电子邮件

时间:2013-12-20 14:23:58

标签: ios iphone

在我的申请中,我正在尝试将姓名和联系电话号码保存到地址簿中。

但是,保存时(使用以下代码),我收到错误:

/Users/mobility/Desktop/STAXApplication/STAXApplication/AccountsAndContactsViewController.m:739:61: Implicit conversion of Objective-C pointer type 'NSString *' to C pointer type 'CFTypeRef' (aka 'const void *') requires a bridged cast

我使用的代码是:

-(void)addRecord:(NSString *)name email:(NSString *)email number:(NSString *)number
{
    NSString *Name = name;
    NSString *Email = email;
    NSString *Number = number;

    ABRecordRef newPerson = ABPersonCreate();

    ABRecordSetValue(newPerson, kABPersonFirstNameProperty, Name, NULL);
    ABRecordSetValue(newPerson, kABPersonEmailProperty, Email, nil);

    //ABRecordSetValue(newPerson, kABPersonLastNameProperty, @"agrawal", NULL);
    //ABRecordSetValue(newPerson, kABPersonPhoneProperty, CFNu,NULL);

    ABMutableMultiValueRef multiPhone =     ABMultiValueCreateMutable(kABMultiStringPropertyType);

    ABMultiValueAddValueAndLabel(multiPhone, Number, kABPersonPhoneMainLabel, NULL);

    ABRecordSetValue(newPerson, kABPersonPhoneProperty, multiPhone,nil);

    ABAddressBookAddRecord(_addressBook, newPerson, NULL);

    BOOL saved = ABAddressBookSave(_addressBook, NULL);

    if(saved == YES)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Saved" message:@"the contact has been saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }

    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@" Not Saved" message:@"Not saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
    }
}

NSString和const void *之间存在一些转换冲突。怎么做这个转换?我也使用了UTF8String ..但它不起作用..

1 个答案:

答案 0 :(得分:1)

你必须以下列方式桥接演员

ABRecordSetValue(newPerson, kABPersonFirstNameProperty, (__bridge CFStringRef) Name, NULL);
ABRecordSetValue(newPerson, kABPersonEmailProperty, (__bridge CFStringRef) Email, nil);