ios - 如何使用 NSTimer 在特定时间执行操作?

标签 ios nsdate nstimer uilocalnotification

我正在尝试在一天中的特定时间执行某项操作。

我不想使用 UILocalNotification 因为我不希望用户在发生任何事情时离开。

我当前的代码是:

[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(fire:) userInfo:nil repeats:NO];

最佳答案

你可以:

  1. 每次您的应用在 application:didFinishLaunchingWithOptions 方法中使用 [NSDate date] 时提取当前本地时间。

  2. 使用 dateWithTimeInterval:sinceDate 计算您的闹钟的剩余时间。

  3. 使用 interval = that_time_difference 启动一个 NSTimer

我不知道你说的是什么意思:

"I don't want the users to be away of anything happening."

我假设您不希望应用程序未运行时发生任何事情。我唯一能想到的就是推送通知。

关于ios - 如何使用 NSTimer 在特定时间执行操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15329496/

相关文章:

objective-c - 类和父类(super class)之间的双向通信

ios - 如何从itunes connect中删除应用程序,处于准备提交阶段

ios - 如何检测 NSAttributedString 是否包含 NSTextAttachment 并将其删除?

iphone - NSTimer 立即触发

ios - 向左滑动 iPhone 主屏幕,XCode UI 测试

ios - swift - 将字符串转换为 NSDate

ios - 将 [NSDate date] 转换为本地化的 UTC 日期字符串

objective-c - 创建日期范围

ios - dispatch_after 还是 NSTimer?

objective-c - 使用计时器检查 if 内的条件