简体   繁体   English

当应用程序在后台时发送 http 请求

[英]Send http requests when app is in background

I have a function which is sending an HTTP request once in 60 sec, but when I lock phone it stops.我有一个功能,它在 60 秒内发送一次 HTTP 请求,但是当我锁定电话时它会停止。 So, is there a way to send HTTP requests when a phone is locked?那么,有没有办法在手机被锁定时发送 HTTP 请求? I've tried to do it in我试过在

func application(application: UIApplication, performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void)

but it's not worked.但它没有用。

That code seems right to me... It should work as long as the code that you add it in the applicationDidEnterBackground function of your AppDelegate.该代码对我来说似乎是正确的...只要您将它添加到 AppDelegate 的applicationDidEnterBackground函数中的代码,它就应该可以工作。 If the device is locked shouldn't matter from what I've seen.从我所看到的情况来看,设备是否被锁定应该无关紧要。 It's still able to perform tasks if the device is locked (ie. play audio)如果设备被锁定(即播放音频),它仍然能够执行任务

Also make sure that if you are doing anything in the background that the app has the ability to make Background refreshes, if need be.还要确保如果您在后台执行任何操作,应用程序能够在需要时刷新后台。 You can add that in your Capabilities tab under the main project file in Xcode.您可以在 Xcode 主项目文件下的 Capabilities 选项卡中添加它。 I think that might be where your issue stems from.我认为这可能是您的问题的根源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM