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.
Related
I'm new to swift and programming so I'm not sure if or how this is possible, but could anyone tell me if I can designate code to be executed when the user terminates the app from the multitasking menu? I just have a line of code that I would like to execute at that time, but I'm not sure where to put it in my project.
Thanks!
If the user terminates the app from the multitasking menu, your app is killed dead. It is not terminated "in good order". You do not get an event at that time. It's just like the scene in 2001: A Space Odyssey where the scientists are already in suspended animation and HAL pulls the plug on them.
The art of Cocoa / iOS 8 programming is the art of the possible. Adjust your desires to fit the reality of the events you do get. You get an event when the user leaves your app, so if there is something you need to be sure to do, do it then, as you may never get another chance.
As matt said, when the app is terminated from the multitasking menu, none of your code is run, it's just killed.
Try putting your code into the application delegate's applicationWillResignActive(_:) method. This will run any time your app becomes inactive, though. In other words, more often (all the time) than being terminated in the multitasking menu. It'll run when there's an incoming phone call or text, I believe it will run if there is a notification with an alert, it will run when the user presses the home button.
I want to invoke a method daily at a specific time in my iPhone application.
How can I achieve this?
There's not a whole lot you can do with the iPhone SDK and a closed app. You can have it send local push notifications at specific times, but that won't execute your code until the user manually opens your app.
See this related question recently asked: Is it possible to have my closed app run code in iOS?
One thing I've seen apps do (like alarm clock apps, etc) is have you leave your app open and just let the phone fall asleep and then it will still execute code when you want it to. But that only works as long as the user doesn't hit the home button.
Schedule a local notification. This is how most basic alarm apps work. The app needs to be running sometime before the method execution time to schedule a local notification. After the local notification has been scheduled, the app can be closed and the method will be invoked at the specific time.
If you have push notifications configured, then your app does not need to run at all to schedule the method. It can all be done from another application.
Check out this documentation on local and push notifications from Apple.
you cannot run a specific code inside your application when its not running ..iOS allow only limited functionality to be run in background or when app is quit.
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.
I'm working on an alarm/reminder application and want to show a custom alarm view when specified time is reached.
From what I understand, there are various (real and hypothetic) ways of doing it:
UILocalNotification. It would be perfect, but alert customization features are insufficient, e.g. I can't show a custom alert view, I can't play a sound for more than 30sec etc.
Keep application in the background and use NSTimer or other means to schedule execution after some time. Most likely won't work as the app may be killed any time.
Start my app at specified time to receive the notification. As far as I know it's not allowed at all.
After spending several days searching for a solution, I would give up and stick with UILocalNotification, but I DO know an app that shows custom alert view at specific time AND is available in AppStore. The app is named "Alarmed" and from what it looks like (last app view shown on alarm time before an actual alert; alarm still triggers even after the device is restarted) they found a way to start their app at specific time and still pass Apple's review.
Any ideas how this can be done?
Thanks.
It's not possible to show notifications other than what UILocalNotification offers you, unless the app is running in the foreground. (I just tried to install "Alarmed" and it uses a regular UILocalNotification like you'd expect).
I'm reading many things about Local Notifications and the way they can help to update the app badge number.
I'd like to update this badge at midnight, and set its value to a number I can't know before midnight. So I'd like, if possible, to launch a function at midnight that would update/load some datas, check the number to display, and display it on the badge.
Of course, the number to display is not relative to the displayed number, and can be lower or greater.
This is expected to work :
- When app is in background
- When app is in foreground
- When app is not launched
would you know a way to do this, even partially ? If not possible, let say I can make things to know the badge number before midnight... Would this help you for a working answer ? This is not wanted, but if I don't have the choice to make this work, I'll plan to write tons of useless code to do this...
Unless your application fits into one of the "official" multitasking cases (VOIP, location, and background audio), the only way you're going to be able to dynamically set the badge number when your application isn't running is with push notifications... applications can't run code in response to a local notification (until the user presses the "View" button in the notification popup, at least).
See this document for everything you could ever want to know about local and push notifications in iOS.
You can't reliably ensure that your local app will run when it needs to unless you register it as a VoIP app or as requiring continuous location monitoring; either of these, in an app that's not in fact used for VoIP or navigation, will almost certainly result in an App Store rejection.
Your best bet is to send the badge as a push notification. Assuming that a server you control can know what the badge number is supposed to be, it can send a notification with just that badge value—not necessarily an alert popup or sound—to your app. Urban Airship is a good, simple way to send push notifications without having to do a lot of configuration and setup.
You very well may be able to do this. Look at the Apple Guide to running code in the background.