Swift 应用终止时向服务器发送请求

Swift send request to server when app killed

我想在应用程序进入 onBackground 或 onAppWillTerminate 模式时发出请求。我在 AppDelegate 方法中添加了一个观察者,但没有用。只是我想发送请求和推送通知作为响应。我怎样才能做到这一点?我的要求不是新闻也不是音乐,很简单JSON.

`func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    Fabric.with([Crashlytics.self])

    GADMobileAds.configure(withApplicationID: "ca-app-pub-8446699920682817~7829108")

   let onesignalInitSettings = [kOSSettingsKeyAutoPrompt: false]
    //ONE SIGNAL

    OneSignal.initWithLaunchOptions(launchOptions,
                                    appId: "f5854e88-0b58-495f-8d3f-e7899202d",
                                    handleNotificationAction: nil,
                                    settings: onesignalInitSettings)

    OneSignal.inFocusDisplayType = OSNotificationDisplayType.notification;
    OneSignal.promptForPushNotifications(userResponse: { accepted in
        print("User accepted notifications: \(accepted)")
    })


    UIApplication.shared.statusBarStyle = .lightContent



     UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)


    mS.StartTimer()

    //NotificationCenter.default.addObserver(self, selector:#selector(AppDelegate.applicationWillTerminate(_:)), name:NSNotification.Name.UIApplicationWillTerminate, object:nil)

    NotificationCenter.default.addObserver(self, selector:#selector(AppDelegate.applicationWillEnterForeground(_:)), name:NSNotification.Name.UIApplicationWillEnterForeground, object:nil)

    return true

}


func applicationWillTerminate(_ application: UIApplication) {

   //mS.StartTimer()

}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

        self.mS.StartTimer()
    if let vc = window?.rootViewController as? LiveScores{
        vc.getLiveMatches(url : URL(string: "http://test.com/test/index.php/Service/lastLive"))
    }
    completionHandler(.newData)

}


func application(_ application: UIApplication,
                          didReceiveRemoteNotification userInfo: [AnyHashable : Any],
                          fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void){
    completionHandler(UIBackgroundFetchResult.newData)
}

func applicationDidEnterBackground(_ application: UIApplication) {
    if let vc = window?.rootViewController as? LiveScores{
        vc.getLiveMatches(url : URL(string: "http://opucukgonder.com/tipster/index.php/Service/lastLive"))
    }
    mS.StartTimer()

}

func applicationWillEnterForeground(_ application: UIApplication) {
    if let vc = window?.rootViewController as? LiveScores{
        vc.getLiveMatches(url : URL(string: "http://opucukgonder.com/tipster/index.php/Service/lastLive"))
    }
    mS.StartTimer()
}

}`

实际上,当调用 applicationWillTerminate 时,应用程序会在 3-4 秒后被强制终止。 这就是为什么 API 没有被调用的原因。

您可以尝试的一种解决方案是在 applicationWillTerminate 函数的末尾添加睡眠,如下所示:

func applicationWillTerminate(_ application: UIApplication) {


    //call API Here

    // 5 is the number of seconds in which you estimate your request 
    // will be finished before system terminate the app process
    sleep(5)
    print("applicationWillTerminate")
}

如果你想在应用程序进入后台时联系服务器,那当然是可以的。实施 UIApplicationDelegate.applicationDidEnterBackground。有关详细信息,请参阅 Strategies for Handling App State Transitions。这是一个非常常见且受支持的操作,应该是您所需要的。

但是,当应用程序在后台被杀死时,无法联系服务器。 applicationWillTerminate 通常在任何情况下都不会被调用,因为 iOS 4。在 iOS 4 之前,没有 "background" 模式。当用户按下主页按钮时,应用程序在调用此方法后立即终止,这就是它仍然存在的原因。但它在现代应用程序中几乎毫无用处。 (您仍然可以通过在 Info.plist 中设置 UIApplicationExitsOnSuspend 来获得这种古老的行为,但这对解决您的问题没有任何帮助。)

当您的应用程序在后台被杀死时,它就被杀死了。它没有被唤醒,也没有调用任何方法。它被毫不客气地终止并从内存中删除。您无法响应此事件。当用户从应用程序切换器强制退出您的应用程序时,也会发生同样的情况。

我有点好奇为什么当应用程序进入后台时,尤其是当应用程序终止时,您会想要推送通知。当我过去看到人们尝试这样做时,他们通常会尝试使用推送通知来保持应用程序始终启动。这不可能。即使您找到了在技术上可行的解决方法,Apple 也明确禁止。