簡體   English   中英

如何在應用程序快速終止/終止/暫停時調用方法

[英]How to call a method while app is kill/terminate/suspended in swift

我想獲取新事件的列表,因此我需要在事件附近時不斷調用服務。 那么只有我可以通過 API 調用發送我的 uid 和 eventide,然后后端團隊通知您在事件附近。 所以這個服務如何在應用程序終止/終止/暫停時調用,因為這些東西在應用程序在后台運行時。

您可以使用以下UIApplication方法調用此webService

 func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

由於任何原因終止應用程序時,調用此方法。 因此,您可以使用此方法調用webService。

謝謝。

如果在您的應用流程中可行,您可以遵循靜默通知方式,每當您需要調用一個方法時,您都可以向設備發送靜默通知,

  • 設備獲得靜音通知,聽起來不像推送通知,
  • 您的應用程序將在后台喚醒,屆時,
  • 你可以調用你的方法。
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.
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM