电子读卡器不工作
我是目标 C 的新手,我现在的处境是我迫切需要创建一个应用程序。 使用 XCode 4.2,
我在应用程序的一部分中 我将检测 QR 码并获取 NSString 格式的 VCard: 我使用以下代码完成了该功能: -我将以下内容导入到框架中:
AddressBookUI.framework
AddressBook.framework
在我写的.h文件中:
#import <addressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
@interface HellowWorld : UIViewController<ABPeoplePickerNavigationControllerDelegate>{
}
-(IBAction)saveContacts;
@end
在我写的.m文件中:
-(IBAction)saveContacts{
NSString *vCardString = test //where the data will be comming from
CFDataRef vCardData = (CFDataRef)[vCardString dataUsingEncoding:NSUTF8StringEncoding]; ABAddressBookRef book = ABAddressBookCreate();
ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(book);
CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData);
for (CFIndex index = 0; index < CFArrayGetCount(vCardPeople); index++) {
ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, index);
ABAddressBookAddRecord(book, person, NULL);
CFRelease(person);
}
CFRelease(vCardPeople);
CFRelease(defaultSource);
ABAddressBookSave(book, NULL);
CFRelease(book);
}
我使用了这段代码,但它不起作用。
首先它没有编译这一行:
CFDataRef vCardData = (CFDataRef)[vCardString dataUsingEncoding:NSUTF8StringEncoding];
我必须将其更改为这样:
CFDataRef vCardData = (__bridge CFDataRef)[vCardString dataUsingEncoding:NSUTF8StringEncoding];
编译后它在这一行崩溃:
for (CFIndex index = 0; index < CFArrayGetCount(vCardPeople); index++) {
它给出以下绿色错误:Thread1:程序收到信号“EXC_BAD_ACCESS”
。 有什么理由吗?
这也是在地址簿中保存 VCard(NSString 格式)的唯一方法吗?还有其他建议吗?
I am new to objective C and I am in a position where I need create an App urgently . I am using XCode 4.2
in a part of the App I will be detecting a QR code and getting a VCard in NSString format :
I did the function using the following code :
-I imported the following to frameworks :
AddressBookUI.framework
AddressBook.framework
in the .h file I wrote :
#import <addressBook/AddressBook.h>
#import <AddressBookUI/AddressBookUI.h>
@interface HellowWorld : UIViewController<ABPeoplePickerNavigationControllerDelegate>{
}
-(IBAction)saveContacts;
@end
in the .m file I wrote :
-(IBAction)saveContacts{
NSString *vCardString = test //where the data will be comming from
CFDataRef vCardData = (CFDataRef)[vCardString dataUsingEncoding:NSUTF8StringEncoding]; ABAddressBookRef book = ABAddressBookCreate();
ABRecordRef defaultSource = ABAddressBookCopyDefaultSource(book);
CFArrayRef vCardPeople = ABPersonCreatePeopleInSourceWithVCardRepresentation(defaultSource, vCardData);
for (CFIndex index = 0; index < CFArrayGetCount(vCardPeople); index++) {
ABRecordRef person = CFArrayGetValueAtIndex(vCardPeople, index);
ABAddressBookAddRecord(book, person, NULL);
CFRelease(person);
}
CFRelease(vCardPeople);
CFRelease(defaultSource);
ABAddressBookSave(book, NULL);
CFRelease(book);
}
I used this code but it is not working.
First it did not compile this line :
CFDataRef vCardData = (CFDataRef)[vCardString dataUsingEncoding:NSUTF8StringEncoding];
I had to change it to this :
CFDataRef vCardData = (__bridge CFDataRef)[vCardString dataUsingEncoding:NSUTF8StringEncoding];
and after compiling it crashes at this line :
for (CFIndex index = 0; index < CFArrayGetCount(vCardPeople); index++) {
It gives the following green error : Thread1: Program Received Signal "EXC_BAD_ACCESS"
.
Any reasons?
also is this the only way to save a VCard (in NSString format) in the address book ? any other suggestions ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设您启用了 ARC。
在使用
vCardData
对象时对其进行CFRetain
处理是否可以阻止崩溃?数据对象可能只能存活到范围结束。或者可能更好,使用
__bridge_retained
请参阅 ARC 发行说明
I'm assuming you have ARC enabled.
Does
CFRetain
ing thevCardData
object while you use it stop the crash? The data object probably only survives until the end of the scope.Or possibly better, use
__bridge_retained
See the release notes on ARC
将 Vcard 数据导入 Iphone 联系人列表,执行以下操作。
添加
AddressbookUi.framework
添加
Addressbook.framework
包含此标头:
并调用此函数:
Import Vcard data to Iphone Contact List do following.
Add
AddressbookUi.framework
Add
Addressbook.framework
Include this headers:
And call this function: