iOS 中未调用 ABAddressBook 外部回调

发布于 2025-01-01 16:39:34 字数 1836 浏览 2 评论 0原文

我有一个类在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

提赋 2025-01-08 16:39:34

我在我的 iPhone 上对此进行了测试,并验证了回调被调用:

Contacts.h:

#import <Foundation/Foundation.h>
#import <AddressBook/AddressBook.h>

@interface Contacts : NSObject
{
    ABAddressBookRef addressBook;
    NSArray *people;
}

+ (Contacts*)sharedInstance;
- (void)refresh;

void MyAddressBookExternalChangeCallback (
                                          ABAddressBookRef addressBook,
                                          CFDictionaryRef info,
                                          void *context
                                          );

@end

Contacts.m:

#import "Contacts.h"

void MyAddressBookExternalChangeCallback (
                                          ABAddressBookRef addressBook,
                                          CFDictionaryRef info,
                                          void *context
                                          )
{
    NSLog(@"callback called ");
    [[Contacts sharedInstance] refresh];
}

@implementation Contacts

+ (Contacts*)sharedInstance
{
    static Contacts *sharedInstance = nil;

    @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 self;
}

@end

I tested this on my iPhone and verified that the callback was called:

Contacts.h:

#import <Foundation/Foundation.h>
#import <AddressBook/AddressBook.h>

@interface Contacts : NSObject
{
    ABAddressBookRef addressBook;
    NSArray *people;
}

+ (Contacts*)sharedInstance;
- (void)refresh;

void MyAddressBookExternalChangeCallback (
                                          ABAddressBookRef addressBook,
                                          CFDictionaryRef info,
                                          void *context
                                          );

@end

Contacts.m:

#import "Contacts.h"

void MyAddressBookExternalChangeCallback (
                                          ABAddressBookRef addressBook,
                                          CFDictionaryRef info,
                                          void *context
                                          )
{
    NSLog(@"callback called ");
    [[Contacts sharedInstance] refresh];
}

@implementation Contacts

+ (Contacts*)sharedInstance
{
    static Contacts *sharedInstance = nil;

    @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 self;
}

@end
少钕鈤記 2025-01-08 16:39:34

好吧,我迟到了大约四年,这个接口现在已经被弃用了……但以防万一有人仍然关心,就我而言,我发现您需要在主线程上注册回调。如果我在不同的线程上注册,它就永远不会被调用。在文档中没有看到这一点(尽管这可能只是我的阅读理解失败)。

无论如何,尝试一下吧。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文