iphone - 当 iPhone 应用程序到达一天中的特定时间时,我如何收到通知?

标签 iphone objective-c cocoa ipad sdk

我正在开发一个应用程序,需要在特定时间(准确地说是整点过 15 分钟)调用一个方法。有没有办法在不消耗大量 CPU 和电池生命周期的情况下实现这一点?我尝试过搜索,但在任何地方都找不到答案。

最佳答案

他们让这变得非常简单。分配一个 NSTimer 并使用触发日期对其进行初始化,使用:

-(id)initWithFireDate:(NSDate *)date
      interval:(NSTimeInterval)seconds
      target:(id)target
      selector:(SEL)aSelector
      userInfo:(id)userInfo
      repeats:(BOOL)repeats

然后使用以下命令将其添加到运行循环中:

-(void)addTimer:(NSTimer *)aTimer forMode:(NSString *)mode

在运行循环上,例如[NSRunLoop currentRunLoop]。不知道这是否会唤醒设备,或阻止其休眠。

编辑一些示例代码

// start in 5 minutes
NSDate *date = [NSDate dateWithTimeIntervalSinceNow:300];

// fire every minute from then on
NSTimer *t = [[NSTimer alloc]
    initWithFireDate:date
    interval:60
    target:self
    selector:@selector(stuff:) // -(void)stuff:(NSTimer*)theTimer
    userInfo:nil
    repeats:YES
];

// make it work
[[NSRunLoop currentRunLoop] addTimer:t forMode:NSDefaultRunLoopMode];

-(void)stuff:(NSTimer*)theTimer
{
    if ( done ) [theTimer invalidate];
}

关于iphone - 当 iPhone 应用程序到达一天中的特定时间时,我如何收到通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3320859/

相关文章:

php - NSObject 到 json?

ios - 在哪里测试这种情况?

html - 保存 HTML、CSS + 图像以供离线使用

cocoa - NSApplicationDelegate类设计

ios - 局部声明隐藏实例变量 - 属性和综合

ios - 将 GraphicsServices 私有(private)框架添加到 IOS

iphone - 如何解决这个内存泄漏?

iphone - 从 HTML 文件链接到 View

ios - 在iOS 8.1中,当我在UITableView处于编辑模式下单击行的删除按钮时,该行及其上方的内容显示不正确

objective-c - cocoa 。有没有办法完全禁用所有窗口的自动布局/约束?