This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
iOS: Get location update every n minutes
How can I get location updates in the Background every 5 Minutes?
You should read the section Implementing Long-Running Background Tasks of in here.
And then pursue either register for significant location changes or declare itself as needing continuous background location updates.
Furthermore a solution to this problem was given here by wjans.
And I quote:
Found a solution to implement this with the help of the Apple
Developer Forums. I did the following:
•Specify location background
mode
•Use an NSTimer in the background by using
UIApplication:beginBackgroundTaskWithExpirationHandler:
•In case n is
smaller than UIApplication:backgroundTimeRemaining it does works just
fine, in case n is larger, the location manager should be enabled (and
disabled) again before there is no time remaining to avoid the
background task being killed. This does work since location is one of
the three allowed types of background execution.
Good luck :)
Related
This question already has answers here:
iOS do scheduled operation in background or when app active
(3 answers)
Run a task at fixed time everyday
(1 answer)
Closed 2 years ago.
I am creating an app that should check something in regular intervals. (Not a service call just harmless check which won't take much of CPU usage)
To run a code in background for a fixed interval of time, I tried using BackgroundTasks framework. My idea was to schedule one background AppRefresh task and schedule another when the first one gets called. Also in the whole day I tried with this framework, there were no updates. Though I always scheduled to run my job after 5 minutes, I sometimes waited for an hour for updates.
The code is working, I used the console command to simulate the background task and it worked.
I am not sure what framework I should use to achieve this?
This question already has an answer here:
Update location in Background
(1 answer)
Closed 4 years ago.
I'm making a location tracking app. In this app, I need to record user location every 10 seconds. Also, the recording is done in a background service so that the application does not need to be in foreground. To make this, I tried to use Timer but it's not working in background. I've tried so much but it didn't work. Could you help me please?
I found this great tutorial: https://www.raywenderlich.com/5817-background-modes-tutorial-getting-started
I had a similar app to be built some time ago, and I also tried things like you did, but that is very very wrong way to use timer and all to record your location.
First of all, I suggest you to not try to record/ping location explicitly because, it will cause batter drainage. There is something called, significant location change, please go through it in docs. This will trigger you location update in every single significant location change.
But I assume, you really need to ping the location. There is a git repo, link below
Background geo location tracking
Please go though it, it is really nice library out there, I have ever come across, go through its documents. I am sure you will come around and be able to use it for your app. :)
My question involves keeping an app that monitors user interactions in the background, for example time spent in one or the app. The issue arises when you can not have a background process run for more than 10 min or violate Apple's sandbox restrictions. Since I am relatively new to the Apple API, and could not find a direct answer that didn't involve location services or VOIP (which are both interesting options, but my application will not be able to use either viably), I come to ask for options in which I can understand when another app opens, when it closes, when it fetches data, and when user holds phone in certain orientation (ie when people hold their phone at certain angles to read text and etc.) for certain amount of time.
The purpose of this analyzation is to predict an attention span for the user, so I need to be able to run in the background, as the user will not be using my app while it predicts attention span.
My thoughts on this are possibly accessing the system log, and somehow parse previous statements (I don't think sandbox will allow), but inevitably the iOS system will suspend my processes after some point unless I put a timer. There is also the option of having the system wake up my app via opportunistic fetching, but that won't be useful if I don't collect the data.
Keep in mind this is within IOS 11, so it is much more restrictive than previous iterations. I understand this may seem like a complex problem, but even a direction in which to head could be useful to me.
This solution might work, (not recommended since it drains the battery quicker).
Just update your current location, every 10 mins. It will reset the background thread timer.
I have read this answer and I do not believe it has what I am looking for but I a am beginner and I am happy to have someone point out the answer in this link : dispatch_after - GCD in swift?
My goal: set a function to run at 9 AM in the user's time zone (or system's time zone) every single day.
I've used GCD very briefly to delay a function as follows (it works perfectly fine):
var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(10 * Double(NSEC_PER_SEC)))
dispatch_after(dispatchTime, dispatch_get_main_queue(), {
let alert = UIAlertView()
alert.title = "Foo Alert"
alert.message = "foo."
alert.addButtonWithTitle("OK")
alert.show()
AudioServicesPlaySystemSound(SystemSoundID(kSystemSoundID_Vibrate))
});
So I wanted to fire it daily as I've mentioned above but the first thing I am stuck on is swapping out DISPATCH_TIME_NOW for a time zone relevant value? Do I even need to consider time zones or will simply replacing DISPATCH_TIME_NOW with military 09:00 be sufficient?
Also, any advice on the overall goal, scheduling to fire function same time every day would be much appreciated.
I'm also not married to using GCD for this goal but it was the one I ran into the most doing searches.
In short, you cannot generally execute some arbitrary function at some arbitrary time unless the app is still running. The dispatch_after presumes that the app is still running at the scheduled time, which is not generally assured. The dispatch_after is great for "do something in x seconds", not "do something tomorrow at 9am".
Yes, dispatch_after can perform some task on some background thread, but that's very different concept from having the app run in the background (i.e., when the app, itself, is no longer in foreground). Please refer to App Programming Guide for iOS: Background Execution, which enumerates all of the various background mechanisms.
The key technologies for performing something when the app is not currently active include:
Using background fetch, you can opportunistically check for new data on the server (but not per your schedule, but rather at the discretion of the OS).
If your app is serving several very specific tasks (e.g. music app, VOIP, navigation app, etc.) you can register for background operation.
You can schedule a local notification to fire at particular time (though it is incumbent on the user to see the notification and tap on it in order for the app to run). For more information see the Local and Remote Notification Programming Guide.
You can register for push notifications and then your server could push a notification to clients at some time of your choosing (but that requires server-side development).
There are other background tasks that can be configured (e.g. continue network requests in background, request a few minutes to finish some finite length task even if the app is no longer active, etc.), but those seem unrelated to your question.
If you could clarify what you want the app to do at the scheduled time, we might be able to advise what is possible and what the alternatives are. But, in broad brush strokes, those are the basic options you have.
Importantly, you should use dispatch_walltime instead of dispatch_time. The difference: If you set a dispatch_time for "1000 seconds from now" it will run 1000 seconds from now (if your app is running). But dispatch_walltime will calculate which time that is on the user's clock, and will run when the user's clock reaches that time.
So if you set up dispatch_time for 9am tomorrow morning, and I set the clock on my device forwards by five minutes, then dispatch_time will run when my clock displays 9:05am. dispatch_walltime will run at 9:00am. (You'll have to experiment what happens if I change the clock from 8:55am to 9:05am because then running when the clock shows 9:00am is obviously impossible).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a clock in iOS that can be used that cannot be changed by the user
I know in many iOS games that have tasks that take time (i.e. "10 hours until animal done breeding"), one can go into "Settings" and change the time to speed up the completion of the task. In the game I'm developing, I don't want users to do this, so I'm trying to implement a system to prevent this type of cheating. I've put a lot of thought into this, but can't come up with a solution. Basically, my question is how do I keep track of time without relying on the system clock (which can easily be manipulated) for the purpose of preventing users from cheating?
transforming my comment into an answer.
Instead of relying on a user or device, rely on an external source for providing unbiased time. A time server, your server, etc. If you don't trust....
Instead of "X hours of real-world time", would "Y minutes of in-app time" work for you? You can pause/continue a timer when your app suspends/resumes.