【问题标题】:Swift send request to server when app killed [closed]应用程序终止时 Swift 向服务器发送请求 [关闭]
【发布时间】:2018-05-11 08:52:02
【问题描述】:

我想在应用程序进入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()
}

}`

【问题讨论】:

  • 请附上您的代码
  • func applicationWillTerminate(_ application: UIApplication) { myService.startTimer() }

标签: ios swift push-notification appdelegate


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

    实际上,当 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")
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-03
      • 1970-01-01
      相关资源
      最近更新 更多