ios How to run method on every day? - ios

I want to check for update of the stored db every day or every X interval of day, how is it possible to call a method on every X interval of day? I found some code that is for the x interval of seconds, is it feasible to do by the same way? And main thing is it should run in background too. I also though of the way by storing the last updated date in database so that I retrive every time app lunch and compare with present date? Please help me by suggesting the better way.

Since you can't really run any app in background unless you are playing audio, doing something thing with the location or a re an voip client, you will need to do this when the user opens your app.
Just store the last update date in the NSUserDefault

You can not execute your application when it is closed. You can not create background applications or daemons.
Maybe running it on application start would be appropriate for you, but if you need information from the device every day, the user will have to make sure the app is open every day.

Related

Time difference between set automatically and by user in iOS device

I'll show my question by example.
So, f.e., it's 12:00 (set automatically) now, but user decided to change the current time to 12:10 (it won't be really exact time, but user wants so).
Is there any way to get this 10 minutes in code?
One method is to connect to a time server on your own. Or, if you app is running when it changes, you could keep a timer running and look for discontinuities in time.
Possible duplicate of: How can I locally detect iPhone clock advancement by a user between app runs?

Xcode - Run code each day on 00:00

I need a piece of code for Xcode (obj-c) that runs every day at 00:00.
The reason for this is because my server updates data each new day and I would like my app to synchronize with the update.
I was thinking about doing a timer that runs every minute and checks the time (from internet, not the phones time), but it seems like there could be an more efficient way.
Help or pointers really appreciated!
Thanks
Use the calendar app on your mac. Set up a recurring event every day at 0:00. At the point where you pick the type of alert, you can select an option to open a file (as opposed to a pop-up, etc.). Pick your app as the file to be opened.
When the app becomes active, register a local notification (UILocalNotification) to trigger at midnight.
Then, you can perform the updates at the app delegate's -application:didReceiveLocalNotification:
You can then cancel the notification when the application goes to background by calling the UIApplication's method -cancelLocalNotification:
I don't think it's even required to use notification here. Notification is usually on best effort basis and doesn't guarantee the delivery. This can be done in few simple steps.
Save the lastUpdated time in your iOS app. Probably in NSUserDefaults or if you're using local database you can store it there as well.
Everytime user launches the application check how much time has elapsed since last update.
If it's more than 24 hours then make an API call to your server and check if there's any new updates available (which you can determine probably from server timestamp). Get the latest data (possibly only the delta difference in stead of everything).
Update the lastUpdated time in your iOS app.

Build a app to remind 6 times a day in different time a whole year?

I am a beginner and would like to make an app for iOS. I do not know where to start but I am not sure whether you can make an app that remembers you every day for a year at different times (I have time to write them in) .. ? Can you make something which can run in the background as the program 'Reminder'?
I will also have it's different in another selected city ..
Thanks
You can use local notification for that.
See this.
You can set time difference as per your requirement.
And even if your app is not running, or is not in background. It will work.
Like this,
local.fireDate = [calendar dateFromComponents:dateComps];
See this link or this , you will get the hint.
you cant run a program in the background all the time in IOS,
you can do 2 things:
you can add reminders to the calendar, so your app wont need to run all the time, only once to add it.
you can use push notifications and just send push to the users every time you want it, so all calculations will be on some php or java or any other server you want.

how to trigger a download on specific date

In my iPhone application I want to trigger a download every monday of a week, every 10th working day of a month, every 3rd day of a month etc. I did some R & D and found that NSDate, NSDateComponents & NSCalendar classes needs to be used for this scenario. Can somebody help in the same as I am new to date & time usage.
Also by the time when trigger comes if app is not in running state or mobile is switched off.. How to handle these scenarios.
If you device is switched off / out of order / app is not running, you can not do any thing.
Apple would not allow you to launch your automatically, so the question of downloading and saving is bit far.
What you can do is, whenever your app is launched you can read from plist or userdefaults about the time ( 3rd day of month ) and compare to the last saved date, if it is one month away then it is the time to download and show a popup to do so.
Only legal way to do this is to schedule NSLocalNotification to desired date. When notification is fired up and user taps it, app would be started, and there you should start with download.
The documentation for Local Notifications: Local Notifications
You can read this: iOS timed background processing
Can go through this tutorial as well.
I have reading up as well. Something new for me as well. Best is NSLocalNotifications. Have a look at this: Reminder App

Track time spent in application

What should I use to track how long is user using my application on iOS devices?
I think I can do it like this:
Log time on App startup (application:didFinishLaunchingWithOptions:)
Log time on App ending (applicationWillTerminate:)
List item
Send report (pairs of start/exit time) to server and count time spent in app
Do you have any better ideas?
Important is to be network and resources effective
(not to send big amount of data or consume iOS resources on complex computing operations.)
Notice that applicationWillTerminate: is not necessarily called when the home button is pressed (this changed from iOS 3.X to iOS 4.0).
Take a look at applicationDidEnterBackground: and applicationWillEnterForeground:
More here.
I believe that calculating the time on the device itself would be best, considering you want this to be network and resource effective. The calculation of endTime - starTime will be very very fast (depending on your time implementation), and then you would only need to send one piece of data (the time it took) over a network instead of two (the start and end times).
Hope this helps!
N.S.
Seems like a reasonable way to do it. It's efficient enough, and depending on how you see the data, it shouldn't be more than a kilobyte or two, if that. Be wary of privacy issues though—users are really against being logged.
Your method is sound, it will have next to no overhead and get the job done nicely.
Log time on App startup (didFinishLaunchingWithOptions)
send report often with interval of 5min.
send report in applicationWillEnterbackground.(pause timer(invalidate) if u used background thread for it).because if the user enter in other app then they can close ur app by just using home button and with minus button in app manager.so u have to send report here also
resume timer with previous time in applicationWillEnterForeground.
Log time on App ending (applicationWillTerminate)
Send report (pairs of start/exit time) to server and count time spent in app

Resources