iphone - ios4中如何执行网络后台

标签 iphone objective-c cocoa cocoa-touch iphone-sdk-3.0

我们的应用程序不使用默认的电子邮件应用程序发送电子邮件,而是使用 SMTP 实现。现在,当应用程序位于前台时 - 电子邮件将使用 SMTP 连接发送。但是如果我点击发送然后切换到另一个应用程序 - 电子邮件不会被发送。仅当我们恢复到应用程序时才会发送它。通常,我们希望在应用程序处于后台时发送电子邮件。欢迎任何提示。聊天应用程序如何轮询新消息 - 如果我们得到任何有关此的提示 - 我们也可以完成我们的任务。

最佳答案

这是一个好问题。

根据 Apple 关于多任务处理的文档:

In addition to the preceding keys, iOS provides two other ways to do work in the background:

Task completion — applications can ask the system for extra time to complete a given task.
Local notifications — applications can schedule local notifications to be delivered at a predetermined time.

如果您的任务不是一个长时间且连续的任务,例如 VOIP 服务、GPS 服务或音频服务,您可以将其声明为“有限长度任务”,并且您可以在后台完成此任务。请记住,MU 仅从 iOS 4.0 起可用,并且仅适用于 3GS 和 4 设备。您还应该为不支持 MU 的其他设备管理这些任务。

始终根据官方文档,您可以这样做:

You can use task completion to ensure that important but potentially long-running operations do not end abruptly when the user leaves the application. For example, you might use this technique to save user data to disk or finish downloading an important file from a network server. There are a couple of design patterns you can use to initiate such tasks:

  • Wrap any long-running critical tasks with -[UIApplication beginBackgroundTaskWithExpirationHandler:] and -[UIApplication endBackgroundTask:] calls. This protects those tasks in situations where your application is suddenly moved to the background.
  • Wait for your application delegate’s applicationDidEnterBackground: method to be called and start one or more tasks then.

关于iphone - ios4中如何执行网络后台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4447333/

相关文章:

php - Cocoa - 应用程序/强制下载和 NSURLConnection

iphone - 在 iphone 中为固定长度的数据样本创建频谱图

iphone - 是什么原因(以及如何解决)这个奇怪的核心位置错误?

iphone - NSDateFormatter、NSDate、UTC 和奇怪的日期格式

ios - 为什么构建编译失败并且无法识别 *pch 中的 __OBJC__ 宏?

ios - TableView 数组返回 null

iphone - kCGColorSpaceGenericRGB 在 iPhone 上已弃用?

iphone - UIView 触摸问题/问题

ios - 如何有效地使用模糊效果 ios

macos - 基于文档的应用程序中的 NSNotificationCenter