2011-11-12 28 views
1

我試圖使用Cocoa(Mac)和PubSub框架獲取我的Gmail未讀郵件數。我已經看到一個或兩個鏈接顯示使用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); 

任何人都知道我可以得到未讀的計數?

謝謝:)

回答

3

你有兩個問題:一是它有一個解決方案,其中一個似乎是一個永恆的課題。

第一種:Feed刷新異步發生。因此,您需要收聽PSFeedRefreshingNotification和PSFeedEntriesChangedNotification通知,以查看Feed何時刷新和更新。通知的對象將是相關的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: @"[email protected]"]; 
    [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的不處理身份驗證正確飼料。我在http://www.dizzey.com/development/fetching-emails-from-gmail-using-cocoa/看到了這一點,我在我自己的系統上重現了相同的行爲。我不知道這個bug是否是10.7特定的,或者它是否影響以前版本的OS X.

「解決方法」是使用NSURLConnection來執行原始提要XML的已驗證檢索。然後,您可以使用其initWithData:URL:方法將其推入PSFeed。這個非常嚴重的缺點是你實際上並不是PubSubing。您必須運行計時器並在適當的時候手動刷新Feed。

我能夠幫助你做的最好的事情就是提交一個bug:rdar:// problem/10475065(OpenRadar:1430409)。

你應該提交一個重複的錯誤來嘗試增加Apple修復它的機會。

祝你好運。

+0

非常感謝您的幫助和解答。我提交了一個重複的錯誤。 –

相關問題