Perform background tasks when app is terminated

前端 未结 3 662
盖世英雄少女心
盖世英雄少女心 2020-11-29 08:36

I\'m creating an application for my school which should check every n minutes if there is a new mark on the website. To do this when the user login for the first time, the n

3条回答
  •  一整个雨季
    2020-11-29 09:27

    Accutally , the answers is yes. But you should not. We can use Location to archive your goal. Accroding to offical document in here: https://developer.apple.com/library/archive/documentation/UserExperience/Conceptual/LocationAwarenessPG/CoreLocation/CoreLocation.html#//apple_ref/doc/uid/TP40009497-CH2-SW10:

    Note: If your app is terminated either by a user or by the system, the system doesn’t automatically restart your app when new location updates arrive. A user must explicitly relaunch your app before the delivery of location updates resumes. The only way to have your app relaunched automatically is to use region monitoring or the significant-change location service.

    So we can archive it by using Starting the Significant-Change Location Service:

    If you leave the significant-change location service running and your iOS app is subsequently suspended or terminated, the service automatically wakes up your app when new location data arrives. At wake-up time, the app is put into the background and you are given a small amount of time (around 10 seconds) to manually restart location services and process the location data. (You must manually restart location services in the background before any pending location updates can be delivered, as described in Knowing When to Start Location Services.) Because your app is in the background, it must do minimal work and avoid any tasks (such as querying the network) that might prevent it from returning before the allocated time expires. If it does not, your app will be terminated. If an iOS app needs more time to process the location data, it can request more background execution time using the beginBackgroundTaskWithName:expirationHandler: method of the UIApplication class.

    Call location update in willFinishLaunchingWithOptions and applicationDidBecomeActive Then you can excute your own code right after

    [_locationManager startUpdatingLocation];
    

    But is is extremly drain your battery, you may be rejected by app store.

提交回复
热议问题