【问题标题】:example of nsnotification triggering an action when a message comes in with the subject line "WoodViolins"当消息进入主题行“WoodViolins”时,nsnotification 触发操作的示例
【发布时间】:2010-08-31 00:36:39
【问题描述】:

有人可以在objective-c中展示一个例子吗?我有地址簿框架和邮件核心来获取收件箱。我不知道如何让它继续检查新消息并在收到带有特定主题的消息时通知。

以利亚

【问题讨论】:

    标签: objective-c addressbook messages


    【解决方案1】:

    MailCore 无法在事情发生变化时向您发送自动通知。使用此框架,您必须定期 ping 服务器。创建一个 NSTimer:

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

    例如为最后一次看到的消息计数添加一个属性:@property NSUInteger lastMessageCount; 然后编写轮询方法:

    - (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;
    }
    

    您现在可以观察该通知并在每次文件夹更改时收到通知。现在应该很容易根据自己的需要进行调整...

    【讨论】:

    • 通过遍历所有消息并检查它们的主题。使用-messageObjectsFromIndex:toIndex: 获取它们,然后迭代,检查每个主题。您可能还想查看messageListWithFetchAttributes:,也许这会直接在服务器上应用过滤器...
    • NSSet *msgs = [folder messageObjectsFromIndex:0 toIndex:[folder totalMessageCount]-1]; for (CTMessage *msg in msgs) { if ([[msg subject] isEqual: @"hi"]) { ... } }
    猜你喜欢
    • 1970-01-01
    • 2010-11-05
    • 2015-12-23
    • 1970-01-01
    • 2013-10-19
    • 1970-01-01
    • 1970-01-01
    • 2018-09-30
    • 2019-11-07
    相关资源
    最近更新 更多