I m writing ios app on 4.3 simulator and 3.2 xcode
The app every minute with timer NSTimer checks news on some websites and if there are news, the app will make alert message with sound "There are something new on ... website!"
But if i close app or it goes to sleep, timer stops on background. So, i have read many articles like
http://www.codeproject.com/Articles/124159/Hour-21-Building-Background-Aware-Applications
How keep NSTimer when application entering background?
and non of them helped me. my questions:
1) What technologies or tricks should i use to make such app? One good man told me that i need to use only Push Notifications.
2) If to use Push Notifications, do i have to get Developer Program $100/year from apple and stay my Mac book always turned on for creating server for Push Notifications?
thank you!
There are only a few approved uses for running in the background: music, navigation, VOIP. If your app doesn't do one of those things, it can only run in the background for up to 10 minutes, then it will be suspended by the OS.
Push notifications require a server. You either run it yourself, or you use a service like urbanairship.com. A push notification just shows up like an alert on the device. It doesn't make your app wake up, unless the user does it by responding to the alert.
Please read this:
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html
If you haven't paid the $100, you can't run your app on a real device. I don't think the simulator properly simulates background states, so debugging background tasks is going to be pretty difficult.
Related
I'm developing a great feature for my app which requires HTTP requests to be sent periodically in background.
I have read already many articles and discussions here, but haven't come up with a solution because all suggestions I met on stackoverflow.com solve the problem only partially.
Let me explain in details what do I want my application to do.
Consider there is a website with API and I want to send requests (when app is in background mode) periodically to check the state of data on the website. And if the state of data is acceptable I would send Push Notification to user to notify him that data is correct at the moment.
Could someone propose a solution how to implement this idea in iOS app?
On iOS you can't do this, as you've described it. You don't get to schedule tasks to happen at regular intervals when your app is in the background or not running. You also don't get to control whether iOS decides to terminate your app when it's running in the background, so "not running" is a case you'd need to handle (you can't just keep running in the background as for long as you want).
I'm not sure I understand the server side of things though. If your server is manipulating the data until it's acceptable, and it can send push notifications, why does it need to wait for an incoming request from the phone? Why not just send the push when the data is ready? If the app decides what's "acceptable", maybe have the app tell the server what it wants so that the server knows when to send a push.
There are a couple of options that would get close to what you describe. If you implement the "background fetch" feature in your app, iOS will launch the app when it's not running and let it make network calls in the background. There's no guarantee of how often this happens, though. This is described in Apple's background execution docs
The other option is the "silent" push notification. If your server sends one of these, iOS can launch the app in the background to handle the notification. The app could make a network call if necessary. You can send these at whatever time you like, but Apple warns to not overdo it:
Silent notifications are not meant as a way to keep your app awake in the background, nor are they meant for high priority updates. APNs treats silent notifications as low priority and may throttle their delivery altogether if the total number becomes excessive. The actual limits are dynamic and can change based on conditions, but try not to send more than a few notifications per hour.
Silent pushes are described in Apple's push notification docs.
iOS Background Execution Limits
Questions
How do I keep my app running continuously in the background?
If I schedule a timer, how do I get it to fire when the screen is locked?
How do I run code in the background every 15 minutes?
How do I set up a network server that runs in the background?
How can my app provide an IPC service to another one of my app while it’s in the background?
Answer from Apple:
The short answer to all of these is You can’t. iOS puts strict limits on background execution. Its default behavior is to suspend your app shortly after the user has moved it to the background; this suspension prevents the process from running any code.
Official: https://developer.apple.com/forums/thread/685525
I want to make an IOS app in which I want to run the app even the user has killed the app from the main thread. For example a Reminder or Alarm Clock app in which even it is not running but still we get the alerts. Can anyone tell me how to achieve this task in iOS.
You can't run if you have been killed. That is what it means to be killed. The user is in charge of what is running - not you.
But you can schedule a UILocalNotification so that the system will put up an alarm alert for you.
You can schedule local notifications (UILocalNotification) to popup in the future. The notifications can be repeating or one-shot.
If the user taps on the notification (or optional custom actions), the app will be restarted. If not, there is no way to keep your app running long term.
I have also read that an app can remain running in the background by playing a repeating silent audio file, but probably not Apple approved.
I am building an app for a client that requires the ability for users to set an alarm time in the app, then be able to run the app in the background, then when the alarm time is hit display a notification.
I know Apple has some limitations on how long apps can run in the background, so what is the proper method for going about this?
I already know how to use local notifications, so showing a notification when the alarm is actually hit is not an issue. I just want to make sure I am able to enable the app to 'wake up' when the alarm time is hit and call the code for showing the local notification.
I don't believe that your app can wake itself up upon a notification, users wake your app up by interacting with the said notification.
However, you can keep your app running in the background by various methods so you don't have to worry about waking it up. One of these methods is to let the app pretend to be a music player and constantly play some sound clips(just make them silent to not disrupt the users), this way the OS won't terminate your app and will allow it to run in the background.
Doing so of course against Apple's guideline, is highly likely that your app will be rejected from the App Store unless you have very good reasons to convince the reviewers.
My company is right now developing an app that uses similar approach to keep on running, since its an in-house application we don't need Apple to review it.
is it possible to have the App Connection to a server Open even if the App has been Terminated from the Background ?
Here is what I'm trying to accomplish
Twitter Client should keep watching user events . and when that events occur it send a Local notification to the user . the whole idea is to stay a way from Push notification and all servers Problems and costs
so in order to to get Local notification works just like Push notification the connection between my IOS 7 App and Twitter API must be Open all the time
any idea if that even possible ?
No, this is not possible. What you're basically asking for is a daemon functionality which is not available on iOS. The whole point about iOS multitasking is that the OS can (and will) terminate your app if it is in the background and other tasks need the resources. That's the problem Push Notifications solve (to some degree).
You could possibly try background fetch feature on iOS 7 to periodically download content updates for user in background.
I'm making an alarm clock-like app. The user should be notified with a vibration and a sound every 10-60 seconds over a period of about 30 minutes. What is the best approach for this? It's easy enough doing it in the foreground, but how can i continue to run scheduled code in the background? I could use local notifications, but the user doesn't receive them when in "do not disturb" mode.
You should review the documentation about background execution: Background Execution and Multitasking. There are multiple ways of consistently running code in the background, but your app has to meet certain guidelines to use them--in your case, I don't think your app neatly falls into any of the categories Apple describes in Implementing Long-Running Background Tasks. There's always the possibility the submission reviews could approve your app anyway.
To avoid potential app rejection, you should implement your app using local notifications, and include a warning in your app that users should disable "Do Not Disturb" for it to function correctly. Unfortunately, there isn't a way for users to exclude apps from DND, nor is there a public API to manipulate DND.