ios - 无法使用地址簿框架在地址簿中保存姓名和电子邮件

标签 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..但它不起作用..

最佳答案

您必须通过以下方式桥接 Actor

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

关于ios - 无法使用地址簿框架在地址簿中保存姓名和电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20705708/

相关文章:

android - 在没有应用商店和市场的情况下安装 ios 和 android 应用程序的最佳方法是什么?

ios - UITextView 不是从 iOS 中的第一行开始

iphone - UITableViewCell 中的 UIScrollView

iphone - 如何知道xcode中方法的返回类型? - iPhone开发

java - 如何将jar文件包含到iphone应用程序中

ios - xcode 查找 UIImageview 的内容

iphone - 数据未保存到核心数据

iOS 开发 : Updating Free App After Buying

iphone - 非公共(public)选择器 "availableDiskSpace"?

ios - 无法使用纯自动布局约束创建 UIScrollerView