Running apps in background?
Is it possible to keep a socket connection alive in background, to be able to push new data and alert users at all times?
3 Solutions Collect From Internet About “Running apps in background?”
The answer to this question is a definitive yes. If you are in the background state, then you can keep a connection open and process messages from a server.
Unfortunately the complexity here is that you don’t have a lot of control over the state your application is in:
- foreground – The user has tapped your icon and the app is running with the UI visible.
- suspended – The user was previously running your app in the foreground, but suspended it by hitting the home button or receiving a call. Basically your app is ‘freeze dried’ and will remain inactive until it is resumed by the user (starting where it left off) or it is terminated by the OS (see below).
- background – The app was previously running in the foreground but has moved to the background state as a result of something the user has done. Normally your app will move to the suspended state in this case, but there are things you can do as the developer to prevent the instant ‘freeze dry’ and go into the background instead (see below). Your app will also be in the background state if it is woken up for a significant change event.
- terminated – Your app has been unloaded from memory and the next time it starts will be from scratch. This is what happens when you double click the home button and then tap the x next to your app icon. It moves the app from the suspended state into the terminated state. This will also happen if the OS decides it needs room for more recently running apps and your app has been suspended for a long time.
So obviously the trick here is how do I stay in the background state as a long as possible. There are a couple of ways to do this:
- Beg for more time – You can get up to 10 minutes of additional background processing when your app is closed if you ask for it.
- Use UIBackgroundMode – You can declare youself a voip, audio or location app by adding the corresponding UIBackgroundMode value to the pList. There are special requirements for these types of apps which you can check out here.
So these approaches are not without their own problems (getting approved on the store being one of them) and as such I tend to agree with the other answers that using push notifications is probably your best approach for notifying your users. With the notification improvements in iOS5 this is going to be the best user experience going forward.
You can keep a socket connection alive (or do whatever else you want) in the background for about 15 minutes after your app closes. There are also more specialized background processing modes (specifically, audio, voip, and location) which Apple supports if your app fits into one of their supported categories. See here.
If you want to keep sending the user notifications indefinitely, you want to use the Apple Push Notification Service. This allows your app to continue to receive notifications when it’s not running, and it conserves resources since there’s only one connection to the APN service at a time.
You can definitely alert users with local and push notifications.
And as far as I know, you can keep a connection open only for limited time.
Look here for more details.
- Specifying a build action (e.g. install) in Xcode GUI
- Swift protocol with associated type – type may not reference itself as a requirement
- Scenekit camera orbit around object
- How to return a value from a void closure in Swift?
- Fit entire Google Map in zoom level in Swift project
- How to find text segment range in iOS
- Custom SceneKit Geometry in Swift on iOS not working but equivalent Objective C code does
- Uploading images to server doesn't work
- User must be signed in to use the native share box
- dismiss keyboard on tapping on any UIControl objects
- Error while using AVAudioSession in ios
- Why does NSError need double indirection? (pointer to a pointer)
- Can I write an iPhone application in any language other than Objective-C?
- How to edit an default Xcode template?
- Sending audio from a Swift App to PHP Server, and somewhere the audio is lost