How to call a method even if app is not running? iOS - iphone

I am building an app that requires core date to update every Sunday. e.g. it will save "thisWeek"'s data to "lastWeek" every beginning of a new week, and reset "thisWeek" to let user input new data for new week.
My question is how to achieve this even if user is not running the app at all?

Use UILocalNotification with repeatInterval property which reschedule the notification with calendar interval. I suggest to go through following Apple iOS guidelines. Bottom line, your app will be open when notification will occur.
UILocalNotification

Just do the archiving if necessary whenever the user starts the app. If your data is timestamped this should be simple and there is no requirement for the phone to even be switched on.

This isn't possible. Your application cannot execute code unless the user has launched it. You could provide a local notification to prompt the user to reopen the app, however this is a sub-optimal solution.
A better solution would be to timestamp your data and then whenever the user opens the app, regardless of when, you can process all of the data and move it where it needs to go.

Related

Detect sleep date in background on MacOS

I have a problem about detecting sleep date and saving it. The thing is I want to run a counter, when you open the app it always count how much time passed and based on that calculates something. The thing is I want to stop counting if the computer is going to sleep. Is there any way to do this in background if the actual desktop app is not running?
I have tried NSWorkspace.willSleepNotification, but its not called if the app is not running, I also tried to do this in a menu bar app if its only an Agent its also not called, maybe its not possible to do.
You need to improve your question by showing us some code, so that we can help you with what you are doing wrong. I have a background app without a menu bar and I do get these notifications. And yes, you will ONLY get this notification if your app is running. What I usually do I create a background-only app to register those notifications, which I will pass to the main app, via a file or an Apple Event.

How to trigger an action on specific date even if the app is closed?

I am trying to implement a scheduler function in my application using UIDatePicker and dateFormatter so that when a user select a date and time for the action to run on that specific date, the action should occur even if the app is closed, so how can I achieve same?
It is not possible to (guaranteed) run specific code at a certain time if your app is not running.
It is possible to schedule a local NSNotification to inform the user. In case the user triggers your notification, your app is started / woken up by the OS and you can handle it. A nice notification guide can be found here

How to run a ~30sec process in the background every hour (iphone app)

I have an iphone app that has a 30second process that does some network IO. Basically, while the app is in the background, i want this process to run every hour (actually once a day, but if it fails i want it to re-run in an hours time).
With the background features of ios 4, is this possible? If so, how? What are the limitations that i'll come up against?
Thanks so much!
Take a look at Apple's documentation about running code in the background.
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html
There are few different ways of approaching backgrounded tasks. The only apps that can have fully backgrounded processes are "audio", "voip" and "location" apps, and this needs to be declared in the Info.plist.
If your app is not of this type, you'll probably find it difficult to do what you want easily. There are methods which allow you to keep your app alive in the background for a finite period of time (also at that link), but eventually your app will be shut down.
Local Notifications will only prompt the user to open the app - do you really want to have an alert pop-up on the phone every 30 seconds?
I was making some kind of similar research, have a look at this SO answer in case you didn't manage to find it before. Applications like DataMan or Data Usage must have some sort of periodic code execution in the background, so I'm not 100% convinced that what you're asking for is impossible..
I believe that Using Local notifications will help....
check following....
http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/IPhoneOSClientImp/IPhoneOSClientImp.html#//apple_ref/doc/uid/TP40008194-CH103-SW1
An application can create and schedule a local notification, and the operating system then delivers it at the schedule date and time. If it delivers it when the application is not active in the foreground, it displays an alert, badges the application icon, or plays a sound—whatever is specified in the UILocalNotification object. If the application is running in the foreground, there is no alert, badging, or sound; instead, the application:didReceiveLocalNotification: method is called if the delegate implements it.
The delegate can inspect the properties of the notification and, if the notification includes custom data in its userInfo dictionary, it can access that data and process it accordingly. On the other hand, if the local notification only badges the application icon, and the user in response launches the application, the application:didFinishLaunchingWithOptions: method is invoked, but no UILocalNotification object is included in the options dictionary.

Alert User Every 24 Hours To Update Data On App Start Up

One of my apps uses a database that synch's online with another. The online data update pretty frequently, so I'd like to remind users to update their local data every 24 hours after they open the app, not while it's closed.
Is it easier to do this with NSUserDefaults, or is it possible to schedule a Local Notification to execute only if the application is opened?
Thanks
Use NSUserDefaults.
Notifications are specifically designed to work while the app is not running :)
When the app is started, check the time of the last update and then start an NSTimer to fire at the correct time and tell the user to update.
However, is this good ui practice?
Surely it's better for your app to update itself in the background without interrupting the user?

Is there a safe way to schedule an alert for an calendar app?

I want to make a special calendar app, but I am afraid it's not possible to safely schedule an alert for an event.
For example: I set up an alert for an event which starts in 3 months. I want to get notified 2 days before the event starts. In iOS 4 there is multitasking, so my app could run in the background all the time.
But now lets imagine it's a hardcore iPhone user who plays huge memory-intensive games all the time. At some point, iOS might kill my background app. Or the user might restart the device and forget to launch my app. So it could happen that the alert never happens. Bad thing.
Is there a safe way to ensure that an scheduled alert is thrown at the user, just like it is the case with the built in alarm clock app or the calendar app?
I'm going to bring back the EventKit notification - use event kit to schedule a calendar entry with an alert, and embed in there a URL that will open your app.
You could also use local notifications but this way the user will be able to see the upcoming event when reviewing the calendar, and even modify slightly if need be. They can't mess with a local notification once it's in place...
You want to use UILocalNotification for this.
EventKit will make it pop up in the user's calendar, maybe not what you want here.
BTW: Multitasking is really more "fast switching" than backgrounding in iOS... you won't be able to run arbitrary code in background, and you should expect to be killed anytime.