views:

34

answers:

1

Can someone show an example of this in objective-c? I have the addressbook framework and mail core to get the inbox. I don't know how to have it keep checking for new messages and notify when a message comes in with a specific subject.

Elijah

A: 

MailCore can not send you automatic notifications when things change. Using this framework, you'll have to periodically ping the server. Create a NSTimer:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(refresh:) userInfo:nil repeats:YES];

E.g. add a property for the last seen message count: @property NSUInteger lastMessageCount; Then write the polling method:

- (void)refresh:(NSTimer *)aTimer {
  // given a CTCoreFolder *folder
  NSUInteger count = [folder totalMessageCount];
  if (count != self.lastMessageCount)
    [[NSNotificationCenter defaultCenter] postNotificationName:@"FolderUpdated" object:folder];
  self.lastMessageCount = count;
}

You can now observe that notification and be informed on every folder change. Should be quite easy for you to adjust it to your needs now...

Max Seelemann
Ok. But how do I read the subject line?
Elijah W.
by iterating through all messages and checking their subjects. get them using `-messageObjectsFromIndex:toIndex:` and then iterate, checking each subject. You might also wan to check out `messageListWithFetchAttributes:`, maybe that applies filters directly on the server...
Max Seelemann
Can you show me an example?
Elijah W.
`NSSet *msgs = [folder messageObjectsFromIndex:0 toIndex:[folder totalMessageCount]-1]; for (CTMessage *msg in msgs) { if ([[msg subject] isEqual: @"hi"]) { ... } }`
Max Seelemann