Request to server when app in background - iphone

I need that my app will send some data to server every six hours for example. Purpose is that it will send request to server even when app in background. As I know only thinks GPS, Music, Push Notifications work on background. Also, as I know UIApplication method beginBackgroundTaskWithExpirationHandler: works not for a long time after app goes on background. Guys, anybody have idea how to implement this? Thanks a lot!

It's simply not possible within the limits of the current iOS SDK. The only kind of apps that can update their content regularly from the background are Newsstand apps and for them, the interval is 24 hours AFAIK and the entire updating process is largely triggered by Apple.

Unless your app falls into one of the categories you mention, the short answer is you can't. The only exception is for Newsstand apps.
But: what data would you be sending to the server if your app isn't running? If you send data to the server when something happens then the server will always be up-to-date. If the user isn't running the app then, by definition, nothing has happened and the server is still "in sync" with the client. (Yes, this potentially makes the server harder to code.)

Related

iphone: backgrounded app sync webservice

I know this question may be a little bit common and over asked but I cannot find any precise information... so :
Is it possible to have some kind of thread running when the app is in background so I can perform basic sync with my webservice ?
App is in background : I mean the user clicked the Home button, or switched to another app
basic sync : photo upload and download with AFNetworking. I know it has method to continue an HTTPRequest while app is in background, but this is not my point.
My goal would be to make some kind of sync manager, reading a list of photos to update created while the user was on the app, and perform those changes.
I know that the manager could be killed by the OS, but since my server uses atomic transfers it is not a problem. I just need a way to relaunch it... Push ?
I think apps like Google Latitude or Mail and those kind of apps uses what I am looking for but I cannot find any relevant details on it. And using iOS5 is not a problem but waiting for iOS6 would not be a solution.
Thank you for your replies !
PS : well I almost forgot. the app is designed for an enterprise program, so maybe rules are different ? I don't think there is any check for in-house deployment so it might lead to new possibilities...
Apple's Mail client has a background daemon which keeps it running but you can't have that with your own applications. Once an app enters a background state, it must halt it's operations. You can request for a little more time when backgrounded to finish off any transfers or writes to disk (see the Executing a Finite-Length Task in the Background section on Apple's Multitasking Guide)
Google Latitude has events generated based on location. This is a special type of backgrounding introduced by Apple for certain types of applications (see Implementing Long-Running Background Tasks section on Apple's Multitasking Guide) but this can't be used for HTTP syncing. It can only be used for audio, location, voip, newstand content, bluetooth and external hardware attachments.
Push doesn't seem like a solution because it only generates an alert. It doesn't trigger any action until the user triggers the opening of the notification.
You'll want to read Tech Note 2277 Networking and Multitasking.
Basically you have a couple of options:
If you can convince Apple that your app is a VoIP app then you can register a VoIP socket and the OS will resume your background app whenever there is activity on that socket.
Your main option though is to register a background task for any outstanding activity that you have to do when your app is put in the background. You typically get 10 minutes to finish up that work.
Mail is a special app with privileges you don't get.
Apps like Latitude typically register themselves for location updates, specifically to be woken up when there are major geo-position changes. Apps that record GPS tracks do similar things.
Found it !
Using Suhail Patel 's link on Apple's Multitasking Guide I added the voip tag to UIBackgroundModes in Info.plist and use setKeepAliveTimeout:handler: method of UIApplication to relaunch it if needed once the app is going to sleep.
I hope this will help a lot of you !
Of course this app won't be allowed to be on the App Store but for in house development this is in my opinion the best way to do so.
Thanks everyone for showing me the right direction !

Send data through socket every x minutes

I have a location based iOS app and I want to interact with a server every x minutes.
I read this post: Send Data Every 10 minutes
But I have to send an empty header to my server even if there is no location update for a while because I have to read any pending commands from my server and it only writes back if you write first.
This has to work even if the app is running background.
What should be the best way to perform this? Using a timer?
I've already made this kind of stuff on Android but I'm a little stuck on how to do here.
Cheers,
Thierry
You can use a NSTimer when the app is running, but you can't do that when the app is in background. The iOS reference contains all the possible uses of multitasking: link
As you can see, there's a small amount of possible operations (voip, audio or location) that are able to wake up your app.

iPhone sdk, Running app in background and send frequent http request

I am trying to take advantage of iPhone 4 multitasking feature. I want to run app in background and frequently send http request to send/receive data, is it possible?
Surprisingly, there is actually very little time-slicing going on in the "multi-tasking" iOS 4.x. What goes on instead is really just application suspend / resume. When an application is sent to the background upon the user tapping the home button, it will stop getting execute cycles after a short while (*).
(*) There are a few exceptions. Applications which declare themselves as "VoIP providers", location-based apps, and music apps can get background execute cycles, presumably only though to perform those very specific operations in response to corresponding events (like a "significant change in location" occurring). Some apps apparently try to stretch the rules and find undocumented or not-strongly-documented techniques to continue getting background execute cycles.
And the "requested running time" that Undeadlegion mentioned is limited in duration.
The reality is that on iOS4, applications are not allowed to run continuously in the background.
#Undeadlegion has suggested a plausible way to achieve your goal. To be more clear, take a look at my previous answer to a similar question at SO, iphone - Connecting to server in background
It is possible to request running time while your app is backgrounded.
See Multitasking Developer's Guide
Although, depending on the intent of your http requests, push notifications may be a viable alternative.
This may be necessary because you aren't providing audio, voip, or location services, so your app can't run in the background indefinitely.

iphone - Connecting to server in background

I'm creating an app which connects to server and sends some text.
If network (both wifi or 3g) is there, it will immediately send the text to server.
But if there is no network, it keeps on polling for server connection every 5 minutes.
All this part is working fine.
But when using iPhone 4 device, i want the app to check for server connection even when app goes into background. So, when app goes to background and when network comes back, it must be able to send the text to server.
How can I achieve it? I've seen some apps where they say that the app will upload photos to server even in background. How will they do it?
I suggest you read this article from Apple carefully, especially the Completing a Finite Length Task in the Background section.
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html
Something to clarify:
Once your app is in the background and is frozen by the OS, there would be no way for your app by it self to wake up and re-connect to the internet.
However, according to the article above from Apple, you can call this beginBackgroundTaskWithExpirationHandler method from your app's delegate to apply for additional time when put in the background, which is to say, though your app cannot wake up by it self when in background, it can, when in the background and not frozen, try to apply for additional time to finish its lengthy task.
Hope it helps.
There is a trick that I think flayvr is using.
If you download and use the app, you will see that they require you to enable your location.
And why is that?
because they want like you to do something in the background even when the app is terminated (they creating an album out of your newly captured photos), and how do they do that?
They use the significant location change, where when someone is traveling some significant distance (something like 500m) each app that registered for significant location change will get awaken for a limited amount of time to perform some quick task and will be terminated in a few seconds.
So your app can register to that event also and when the event of significant location change fired you will be able to send the text to server (quickly).
Hope that helps.
Until now you can do that on iOS7 with Background Fetch.
Take a look at this article.
However you only have up to 30s to get the task done.
According to the article above, there's also another solution called Background transfer service.
If more time is required though, then the Background Transfer Service
API can be used
Create a new project in Xcode and you will see there are bunch of new methods auto generated in app delegate file. like applicationDidEnterBackground, applicationWillEnterForeground etc.
read the description you have to call your thread to upload data on server here.

Question regarding iphone allowance of custom notifications

I have an app that I am working on, and part of its main functionality is to alert the user to do something. So even though they aren't in the app, I want to be able to pop up an alert from the app, reminding them to do something. Is that allowed with the iPhone sdk? Maybe it isn't even a big deal at all, but for some reason I was thinking that it would require background processes or something that may or may not be possible. Anyone who has any thoughts about how this would be implemented would be greatly appreciated. Thanks!
Check out the Apple documentation on push notifications. Short of jailbreaking and distributing your app outside the App Store, there is no way to run background processes at this time.
It is allowed through notifications, though you will need a server to actually send the notifications to the user. There is no limit to the number you can send, although the user may very well decide to shut off notifications or delete your app altogether if you are too spastic with them. Make very sure the user understands the notification volume they will receive and agrees to it...
The way the system works is the user gets a notification message (and/or sound) and can decide to open your app or not, as the time of notification.