简体   繁体   English

如何在iOS应用程序的后台发出间歇性的HTTP POST请求-Swift

[英]How to make intermitent HTTP POST requests in the background of an iOS app - Swift

Hi I'm making an app that sends POST location update requests to a web application. 嗨,我正在制作一个将POST位置更新请求发送到Web应用程序的应用程序。 I have one function called locationUpdate that I'd like to call intermittently once the app is in the background however I have't found a way to do it yet. 我有一个名为locationUpdate的函数,一旦应用程序在后台运行,我想间歇地调用它,但是我还没有找到一种方法来执行此操作。

I have all the basics like Allow Location Always set. 我具有“允许始终设置位置”之类的所有基础知识。 The app currently makes one HTTP POST request right when it enters the background because of this code in the AppDelegate file: 由于进入AppDelegate文件中的以下代码,该应用程序当前在进入后台时立即发出一个HTTP POST请求:

 func applicationDidEnterBackground(_ application: UIApplication) {

    sendLocation.determineCurrentLocation()
}

I also have this in the AppDelegate because I've seen it in a few stack overflow answers: 我在AppDelegate中也有此功能,因为我已经在一些堆栈溢出答案中看到了它:

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    UIApplication.shared.setMinimumBackgroundFetchInterval(UIApplicationBackgroundFetchIntervalMinimum)
    return true
}

So all this but the app doesn't call sendLocation.determineCurrentLocation() (The function that makes the POST request) more than once and that is when the app is closed. 因此,除了应用程序外,所有这些都不会多次调用sendLocation.determineCurrentLocation()(发出POST请求的函数),也就是关闭应用程序时。

Can anyone tell me how to implement intermittent calling of the sendLocation function? 谁能告诉我如何实现sendLocation函数的间歇调用?

EDITED to show my locationManager: 编辑以显示我的locationManager:

 func determineCurrentLocation(){

    locationManager.delegate = self
    locationManager.allowsBackgroundLocationUpdates=true
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()


    if CLLocationManager.locationServicesEnabled(){
        DispatchQueue.main.async {

            self.locationManager.requestLocation()
        }

    }
    else if (!CLLocationManager.locationServicesEnabled())
    {
        print("You need to enable location services to use this app")

    }




}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
    locationManager.stopUpdatingLocation()
    userLocation = locations[0] as CLLocation
    locationStruct.latitude=userLocation?.coordinate.latitude
    locationStruct.longitude=userLocation?.coordinate.longitude



    print(userLocation?.coordinate.latitude)
    print(userLocation?.coordinate.longitude)

    sendLocationPost()


}

You won't be able to do what you are trying to do unless you set up your app as a turn-by-turn navigation app and ask for permission to get location updates in the background. 除非您将应用程序设置为逐步导航应用程序,并要求获得权限以在后台获取位置更新,否则您将无法做您想做的事情。

Apps only get a very short time in the background before they are suspended. 在暂停应用程序之前,应用程序只会在很短的时间内在后台运行。

You can ask for more background time with the beginBackgroundTask(expirationHandler:) call, but that's normally limited to 3 minutes of background time at max. 您可以使用beginBackgroundTask(expirationHandler:)调用请求更多的背景时间,但是通常最多限制为3分钟的背景时间。 (Search on that method to find the docs about it.) (搜索该方法以查找有关它的文档。)

Only certain types of apps are allowed to run indefinitely in the background. 仅允许某些类型的应用程序在后台无限期运行。 (Turn-by-turn navigation apps and streaming music apps.) You have to set up your info.plist to declare that your app is one of those types of apps. (转弯导航应用程序和流音乐应用程序。)您必须设置info.plist,以声明您的应用程序是这些类型的应用程序之一。 That is documented in the iOS docs that come with Xcode. Xcode随附的iOS文档中对此进行了记录。

EDIT: 编辑:

As Paul points out in his comment, you CAN ask for "always" location update permission and then ask for significant location change updates, although those are rather crude and you can't control the time interval between location updates (and won't get any updates until the OS decides the user has moved a significant distance.) 正如Paul在评论中所指出的那样,您可以请求“始终”进行位置更新许可,然后请求进行重大的位置更改更新,尽管这些更新非常粗糙,您无法控制两次位置更新之间的时间间隔(并且不会获得任何更新,直到操作系统确定用户已经移动了很远的距离。)

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

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