how to run code logic in the background in swift - ios

I am new to swift. Here is the following thing I am trying.
Track the location and motion status of the user in the background. When user's motion status goes from walking to running I want to track the location. And when the app comes to foreground I want to pin the tracked location where the user started running from walking.
It works fine when the app is in foreground entire time. But it does not work when the app is in background.
How shall I implement the code for the logic to be executed in the background?
Location service in the background:

Related

invoke a alarm sound when ios app is in background and someone pick it, using core motion

invoke a alarm when someone touched the iPhone or iPad and a app is running in background.
this is happening into a ios app i.e. iAntiTheft
iAntiTheft is not using the location i checked the permission of the app, in a scenario i put the app in background activating motion alert and device get's locked after some time, now i pick the device, it immediately show a Notification and start alarm on high volume.
one More thing here to notice it always playing a low volume beep beep after enabling the motion sensor alert. any help appreciated.
how can i achieve this.
The answer to get Core Motion Update when the app is in background and the device locked. in both case just put an audio with background mode playing after the core motion updates function, now the device will go to background or get locked you always getting the core motion coordinates updates
Repo Link Providing soon

iOS - Background Services when app is terminated

Do background mode functions like Background fetch and Location update work if the app is terminated? Or it only works if the app enters background?
Thanks
Yes, it works (most of the time), if you set up everything correctly and have the permissions. Your app need's to be launched at least 1 time, so it can subscribe to the updates.
For background fetch, set UIApplication.shared.setMinimumBackgroundFetchInterval(3600) at the didFinishLaunching method, implement the performFetchWithCompletionHandler method, and enable the Background Fetch in the Background Modes.
Pay attention to do it as quickly as possible, and call the completionHandler as soon as possible.
Read more on Updating Your App with Background App Refresh here
For notification updates, you must also set the allowsBackgroundLocationUpdates property of your CLLocationManager object to true, and enable the Location updates in the Background Modes.
Read more on Handling Location Events in the Background here
Background fetch works like, it allows the app to download the contents when it is background. If the app is terminated and gets some trigger to download content, it will actually wake up by doing silent-launch of the app in the background and download the contents. Please see the Apple description on this below.
Each of the preceding modes lets the system know that your app should
be woken up or launched at appropriate times to respond to relevant
events. For example, an app that begins playing music and then moves
to the background still needs execution time to fill the audio output
buffers. Enabling the Audio mode tells the system frameworks that they
should continue to make the necessary callbacks to the app at
appropriate intervals. If the app does not select this mode, any audio
being played or recorded by the app stops when the app moves to the
background.
Here, preceding modes refer to Background fetch, Audio and AirPlay, Location updates and other Background modes of the app.
Please refer Apple document on Background Execution. See Declaring Your App’s Supported Background Tasks for more info on different background modes.
Location update works differently. There are multiple Apple services available to fetch location.
Significant Location service: It works in all modes. Foreground, Background and even in terminated mode.
Standard Location service: It works only in FG and BG mode. It does not work when the app is in terminated mode.
On more details on Location in BG, please refer Handling Location Events in the Background document.
Hope it helps.
Background fetch and Location update work if the app is terminated? Or it only works if the app enters background?
It depends on which type of location service you have used in the project. Refer below analysis of all types of location services.
Standard location service: If you implemented standard location service then it will work only for background and foreground
state.
Significant location updates: If you implemented significant location updates then it will work for background, foreground and
terminate state as well.
Region Monitoring: If you implemented significant location updates then it will work for background, foreground and
terminate state as well.
Visits Location Service: If you implemented Visits Location Service then it will work for background, foreground and
terminate state as well.
Please refer below references.
Apple official doc
Raywenderlich article

Stop Updating Locations on ios when user clicks stop button

I am using location services in my ios app for tracking purposes. The app tracks location in foreground as well as background, until the app terminates.
Here all the code is written in react-native java script except the beacon tracking API values which are being sent from native ios code through app delegate and location manager.
The app updates locations in all cases throughout the life cycle of app but sends beacon values to API only after a specific track interval. There is a stop button, on clicking of which the app stops sending location values to API but the location manager still continues to change lat long values.
What i want is, the clicking of stop button should stop the location manager updates and should start only when i click on 'start' button. This can be handled through app delegate methods but they are called only when any app transition happens like switching from foreground to background or vice versa.
They are not called if i switch between screens remaining on foreground only. And all the screens are handled through react code only, not in native ios.
Suggest something to control the location manager activities in the manner mentioned above, please.

How to create iPhone app without user operation in response to the iBeacon detection

Is it possible the following behaviors on my iPhone app ?
1) Proccess begins in background ,when detecting iBeacon wave.
2) Proccess begins with a screen, when detecting iBeacon wave, but
the application will finish automatically(without any user operations) after proseccing.
To simply put it, no. In order for your code to execute in the first place, the user needs to tap on the app. That sort of style application MAY be able to work on Android, but not on iPhone.
You CANNOT start an application from the background.
Also, when it's in the background, you CANNOT bring its UI up and obstruct what the user's trying to do.

Location services switching off in background in iOS

I have enabled background mode for location services in my app, but when I send the app to the background, it seems that the location services are switched off after a few seconds (the arrow of location services in the device's status bar disappears). I'm not programmatically stopping the location manager, and I'm using standard location service with its best accuracy.
What can I be missing?
Thanks in advance
For iOS 7 and above, When the app is on the background and not executing code. Normally, the system will move app from background mode to suspended mode automatically without any notification. When you app is in suspended mode, you can not do anything (no location update).
In order to keep the app active in the background for iOS 7 and above, you will have to know when to restart the locationManager.
I have a blog post with a detail explanation and also a complete solution uploaded to Github on how to keep the app active in the background, please check this post for more details:- Background Location Services not working in iOS 7

Resources