简体   繁体   中英

example of nsnotification triggering an action when a message comes in with the subject line “WoodViolins”

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

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];

Eg 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM