0

I have tried to call api in app terminated condition.

My requirement is send location(lat-long) through api call. I am able to send location in foreground and background mode but not in app terminated mode like user killed the app.

I know Apple wont allow this as i found many answer in this topic. But my question is there are some apps who can do this. Like Life360. This app can send location if the app killed mode.

I saw my family member in the map location though his app is not running.

Maelig
  • 2,046
  • 4
  • 24
  • 49
Raj_Ogma
  • 1
  • 2

2 Answers2

0

Keep BackgroundModes On for Location Updates via XCode settings.

  • Then use combination of startMonitoringSignificantLocationChanges() and regionMonitoring.

  • Create a region when your app is going in background mode / being terminated.

  • In the didExitRegion , start updating location.

  • Whenever there are significant location changes, you app will receive updates via delegate methods

  • When your app is in terminated mode and app receives location updates, it will be received in didFinishLaunching method's launchOptions?[UIApplicationLaunchOptionsKey.location]

Shantanu
  • 3,086
  • 3
  • 25
  • 32
  • can you guide me step by step? I know it seems weird but i am so much confused. Background location fetch is on right now. – Raj_Ogma Jun 10 '19 at 08:14
  • @Raj_Ogma : I understand you are confused, but no one here will guide you step by step with the solution. Instead, can just try to go one step at a time with above solution, and you will be pretty close to the solution. – Shantanu Jun 10 '19 at 08:21
  • Thank you Shantunu. I have two questions: #1- If i switch on startMonitoringSignificantLocationChanges then didUpdateLocations delegate will be called? and #2 - can you elaborate your point number 5 – Raj_Ogma Jun 10 '19 at 08:27
0

Well, it's not preferable but you could use NotificationCenter by adding an observer or you could use UserNotifications and schedule you'r request for specific delivery time.

  • Can you guide me with some code? It will help me. I am new in this section. – Raj_Ogma Jun 10 '19 at 08:13
  • You can do something like `NotificationCenter.default.addObserver(self, selector: #selector(self.applicationSetup), name: UIApplication.willTerminateNotification, object: nil)` and on the function **applicationSetup** you can do what ever you want – Tetrafluoroethylene Jun 10 '19 at 08:39
  • i want this as repeat job. it seems it will call once. – Raj_Ogma Jun 10 '19 at 08:44
  • Then you will have to use `UserNotifications` postpone a local notification with turning repetition on – Tetrafluoroethylene Jun 10 '19 at 09:59
  • @Raj_Ogma Take a quick look at the following article, to know how to use local notifications with Swift. https://medium.com/quick-code/local-notifications-with-swift-4-b32e7ad93c2 – Tetrafluoroethylene Jun 10 '19 at 13:35
  • Local notification can be fired only once and cannot be coded as repeat job. as it will crash on this line. You can try and let me know. :( – Raj_Ogma Jun 12 '19 at 06:37