【问题标题】:How do I repeatedly call a method when my iOS 4 app is in the background?当我的 iOS 4 应用程序在后台时,如何重复调用方法?
【发布时间】:2010-12-16 16:19:34
【问题描述】:

我正在为 iOS 4 编写一个应用程序,该应用程序需要在后台以定期(用户指定的)间隔播放声音(或振动)。我不想使用本地通知,因为不需要出现可点击的警报。

当我的应用切换到后台时,会触发此代码(来自 Apple 的文档):

UIApplication *app = [UIApplication sharedApplication];

bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];

// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // Do the work associated with the task.
    NSLog(@"I am now in the background");

    // Here I need something like this:
    [self performSelector:@selector(myMethod) withObject:nil afterDelay:5.0];

    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
});

[self performSelector: ...] 行在应用程序切换到后台时执行,但在应用程序切换回前台之前不会调用 myMethod 方法(不是在 5 秒内,如在 afterDelay: 中设置的那样范围)。那么,如何在应用仍在后台时调用该方法?

【问题讨论】:

  • 您能否告知您的应用程序将在后台保留多长时间?我读到它最多 10 分钟.. 是对还是错?

标签: loops audio ios4 background multitasking


【解决方案1】:

没关系!安排本地通知可以满足我的需要。

最好使用本地通知和UIApplication cancelAllLocalNotifications 来清除用户尚未关闭的现有通知。完美的!另外,如果弹出消息让用户知道声音(或振动)的用途,用户可以获得额外的好处。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 2020-01-25
    相关资源
    最近更新 更多