ios - 如何在应用程序快速终止/终止/暂停时调用方法

标签 ios swift

我想获取新事件的列表,因此我需要在事件附近不断调用服务。然后只有我可以通过 API 调用发送我的 uid 和 eventide,然后后端团队通过事件通知你在附近。 所以这个服务如何在应用程序终止/终止/暂停时调用,因为这些东西在应用程序在后台运行时工作。

最佳答案

如果您的应用程序流程可行,您可以遵循静默通知方式,每当您需要调用方法时,您可以向设备发送静默通知,

  • 设备获得听起来不像推送通知的无声通知,
  • 您的应用将在后台唤醒,并在那时
  • 您可以调用您的方法。
func registerForNotification() {
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in
            guard error == nil else { return }
            if granted {
                //Register for RemoteNotifications. Your Remote Notifications can display alerts now :)
                DispatchQueue.main.async {
                    UIApplication.shared.registerForRemoteNotifications()
                }
            }
            else {
                //Handle user denying permissions..
            }
        }
        providerDelegate = ProviderDelegate()
        pushRegistry.delegate = self
        pushRegistry.desiredPushTypes = [.voIP]
    }
func pushRegistry(_ registry: PKPushRegistry, didUpdate credentials: PKPushCredentials, for type: PKPushType) {
//Here you get the token, send it to server.
}

关于ios - 如何在应用程序快速终止/终止/暂停时调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59089247/

相关文章:

ios - 单击按钮时无法播放一系列图像

ios - 如何在用户位置上居中 map IOS 9 SWIFT

ios - 神奇记录自定义核心数据迁移不拉现有数据

html - UIActivityViewController 超链接

ios - 平铺 PDF View 正在泄漏内存(非 ARC)

swift - 在 iOS8 中转换 LAError EXC_BAD_ACCESS

ios - storyboard.instantiateInitialViewController 返回错误的 uiViewController

ios - Mac Catalyst 文本框第一响应者

iOS - 友好的 NSDate 格式

ios - 无法初始化 PFObject 子类