iOS 中未调用 ABAddressBook 外部回调
我有一个类在 iPhone 应用程序中保存用户的联系人列表。
其核心实现如下。
//file Contacts.m
//imports here
void MyAddressBookExternalChangeCallback (
ABAddressBookRef addressBook,
CFDictionaryRef info,
void *context
)
{
NSLog(@"callback called ");
[[Contacts sharedInstance] refresh];
}
@implementation Contacts
@synthesize addressBook;
+ (Contacts*)sharedInstance
{
@synchronized(self)
{
if (sharedInstance == nil)
{
sharedInstance = [[Contacts alloc] init];
}
}
return sharedInstance;
}
- (void)refresh
{
ABAddressBookRevert(addressBook); /*refreshing the address book in case of changes*/
[people release];
people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
}
- (id)init
{
if ((self = [super init]))
{
sharedInstance = self;
addressBook = ABAddressBookCreate();;
people = nil;
[self refresh];
ABAddressBookRegisterExternalChangeCallback ( addressBook,
MyAddressBookExternalChangeCallback,
self
);
}
return sharedInstance;
}
在 init
中,我设置了一个外部回调 MyAddressBookExternalChangeCallback
来接收联系人列表更改的通知。 我遇到的问题是,当我在 iPhone 手机应用程序中添加联系人时,不会调用外部回调(我从未看到 nslog 消息的结果)。
我做错了什么?
添加注释:更奇怪的是,如果我
ABAddressBookRevert(addressBook);
每次我的应用程序来到前面并且地址簿已被修改,那么回调就会被调用。
I have a class holding the list of contacts of my user in an iPhone app.
The core of it is implemented as follows.
//file Contacts.m
//imports here
void MyAddressBookExternalChangeCallback (
ABAddressBookRef addressBook,
CFDictionaryRef info,
void *context
)
{
NSLog(@"callback called ");
[[Contacts sharedInstance] refresh];
}
@implementation Contacts
@synthesize addressBook;
+ (Contacts*)sharedInstance
{
@synchronized(self)
{
if (sharedInstance == nil)
{
sharedInstance = [[Contacts alloc] init];
}
}
return sharedInstance;
}
- (void)refresh
{
ABAddressBookRevert(addressBook); /*refreshing the address book in case of changes*/
[people release];
people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
}
- (id)init
{
if ((self = [super init]))
{
sharedInstance = self;
addressBook = ABAddressBookCreate();;
people = nil;
[self refresh];
ABAddressBookRegisterExternalChangeCallback ( addressBook,
MyAddressBookExternalChangeCallback,
self
);
}
return sharedInstance;
}
In init
, I setup an external callback MyAddressBookExternalChangeCallback
to be notified of changes to the contact list.
The problem that I have is that the external callback is not called (I never see the result of the nslog message) when I add a contact in the iPhone phone app.
What did I do wrong ?
Added note: even stranger, if I do
ABAddressBookRevert(addressBook);
every time my app come to the front and the address book has been modified, then the callback is called.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在我的 iPhone 上对此进行了测试,并验证了回调被调用:
Contacts.h:
Contacts.m:
I tested this on my iPhone and verified that the callback was called:
Contacts.h:
Contacts.m:
好吧,我迟到了大约四年,这个接口现在已经被弃用了……但以防万一有人仍然关心,就我而言,我发现您需要在主线程上注册回调。如果我在不同的线程上注册,它就永远不会被调用。在文档中没有看到这一点(尽管这可能只是我的阅读理解失败)。
无论如何,尝试一下吧。
Welp, I'm about four years late to the party and this interface has now been deprecated... but in case anybody still cares, in my case I found that you need to register for the callback on the main thread. If I registered on a different thread, it just never got called. Don't see that in the docs (though maybe that's just a reading comprehension fail on my part).
Anyway, try that.