Macos 使用PubSub获取Gmail未读计数

Macos 使用PubSub获取Gmail未读计数,macos,cocoa,publish-subscribe,Macos,Cocoa,Publish Subscribe,我正在尝试使用Cocoa(Mac)和PubSub框架获取我的Gmail未读邮件数。我已经看到一两个使用PubSub和Gmail的链接,这是我目前的代码 PSClient *client = [PSClient applicationClient]; NSURL *url = [NSURL URLWithString:@"https://mail.google.com/mail/feed/atom/inbox"]; PSFeed *feed = [client addFeedW

我正在尝试使用Cocoa(Mac)和PubSub框架获取我的Gmail未读邮件数。我已经看到一两个使用PubSub和Gmail的链接,这是我目前的代码

PSClient *client = [PSClient applicationClient];
NSURL    *url    = [NSURL URLWithString:@"https://mail.google.com/mail/feed/atom/inbox"];
PSFeed   *feed   = [client addFeedWithURL:url];

[feed setLogin: @"myemailhere"];
[feed setPassword: @"mypasswordhere"];

NSLog(@"Error: %@", feed.lastError);
有人知道我怎么才能得到未读计数吗


谢谢:)

你有两个问题:一个是有解决方案的问题,另一个似乎是永久性的问题

第一个:提要刷新是异步进行的。因此,您需要收听PSFeedRefreshingNotification和PSFeedEntriesChangedNotification通知,以查看提要何时得到刷新和更新。通知的对象将是有问题的PSFeed

例如:

-(void)feedRefreshing:(NSNotification*)n
{
    PSFeed *f = [n object];
    NSLog(@"Is Refreshing: %@", [f isRefreshing] ? @"Yes" : @"No");
    NSLog(@"Feed: %@", f);
    NSLog(@"XML: %@", [f XMLRepresentation]);
    NSLog(@"Last Error: %@", [f lastError]);


    if(![f isRefreshing])
    {
        NSInteger emailCount = 0;
        NSEnumerator *e = [f entryEnumeratorSortedBy:nil];
        id entry = nil;

        while(entry = [e nextObject])
        {
            emailCount++;
            NSLog(@"Entry: %@", entry);
        }
        NSLog(@"Email Count: %ld", emailCount);
    }
}

-(void)feedUpdated:(NSNotification*)n
{
    NSLog(@"Updated");
}

-(void)pubSubTest
{
    PSClient *client = [PSClient applicationClient];
    NSURL    *url    = [NSURL URLWithString:@"https://mail.google.com/mail/feed/atom/inbox"];
    PSFeed   *feed   = [client addFeedWithURL:url];

    [feed setLogin: @"correctUserName@gmail.com"];
    [feed setPassword: @"correctPassword"];
    NSError *error = nil;

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(feedUpdated:) name:PSFeedEntriesChangedNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(feedRefreshing:) name:PSFeedRefreshingNotification object:nil];

    [feed refresh:&error];
    if(error)
        NSLog(@"Error: %@", error);
}
第二个(也是更糟糕的)问题是PubSub不能正确处理经过身份验证的提要。我在上看到了这一点,并在我自己的系统上复制了相同的行为。我不知道这个bug是10.7特有的,还是它影响了OSX的早期版本

“解决方法”是使用NSURLConnection对原始提要XML执行经过身份验证的检索。然后,您可以使用initWithData:URL:方法将其放入PSFeed中。这有一个非常严重的缺点,那就是你实际上不再发布了。您必须运行计时器,并在适当的时候手动刷新提要

我能为您提供的最好帮助就是提交一个bug:rdar://problem/10475065 (OpenRadar:)

您可能应该提交一个重复的bug,以增加苹果修复它的机会


祝你好运

非常感谢你的帮助和回答。我已经提交了一个重复的bug。