iPhone background network connection by timer - iphone

I need to write an application, that every 10 minutes it should be awaken from suspended mode, get user location via gps and send this information to the server by network.
Depending on the response it should return to the suspended mode or show local notification to the user.
Is there a way to do this on iOS 4?
I've tried different approaches, but the only working for me was to start monitoring user location in backgroind and declare the application as location background application. In that case it worked in background and has a network connection. But this approach takes a lot of power and not accepted cause application should work 24/7.
May be there is a way to write some daemon that should work in background and wake my application every 10 minutes?

Apparently, Pastebot tried to do something similar with the 'audio' multitasking declaration (by playing a silent audiofile) and got rejected.. UNTIL they actually presented a option to the user to pick which audiofile they wanted to play. It's in the appstore now. :)
In this case: What is your reason for not wanting to use the location updates? If battery-usage is a concern, you can use the 'significant location changes only' option, after which you can temporarily change to a more accurate option.

This isn't possible outside of the method you have already tried.
The iPhone background task API will allow you to run a location service in the background.
There is no way to write daemons for the iPhone without jailbreaking, and that is not something I'm able/prepared to help you with.

App store friendly: use new APIs in iOS4, which allows u to make use of GPS location
Anti App store: create a daemon by adding a specific plist file to System/Library/LaunchDaemons/ and put ur app under Applications/. this approach requires a jailbroken iPhone however...
detailed information plz google the following keywords: daemon, multitask, background, etc...
cheers, Lloyd

Related

How to receive acceleration-related data from the onboard hardware, when application in background mode?

Am developing application for calculate count of user moved steps and draw the user activities in the map. And we are using UIAccelerometer delegate for receive acceleration-related data from the onboard hardware. So I need to receive acceleration-related data when the application in background mode too. Last time one of my application got rejected because of using location service in background, Apple suggested me like "you can only use this background mode if your app truly needs this information to provide value for the user". Kindly suggest the best approach for this application.
You cannot run accelerometer in background.
You can track user's location in background... however, if you just keep tracking it and do nothing with that info.. apple is going to reject it and tell you to just get the lates location when app comes to foreground.
what you can try is.. update the user of total distance covered etc on a regular basis (like run keeper) and this will justify tracking location in background.
But first, you can try appealing to the review board explaining that you need to track location in background because you show entire route travelled by user when app comes to foregraound. and compare this to existing apps like run keeper and if you are lucky apple may approve your application without any changes...
This can easily be done.
What happens in that iOS put your application in halt state when your application is not in running in foreground state.
You just need to register your application for background execution.
Refer :
http://dcraziee.wordpress.com/2013/05/20/background_ios/
Also refer :
iOS background application network access
for apple policies about using location service.Which states that you can use location service in background.

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 !

Update my app when it is in background

I want to update some data to my application. Consider the application is in the background state, it is neither Voip or Music or GPS. Is it possible to update/send data to the application which is in background?
NOTE: I dont want to notify the user so that the application becomes active.
Can anyone help me ??
The answer is yes and no.
Apple does allow you app to complete a lengthy process in the background. But if you does not fall in the Voip, music or GPS category then you can't run in background.
If for example you want to send some data to a server, which could take some time, then you can mark that process to back executed until it is finished (or 10min. have passed).
You will find some about Executing a Finite-Length Task in the Background
There is not way to run timers or any thing like that in the backgroud, you can only finish a task you started before the app is backgrounded.
The alert which is displayed is an inbuilt functionality. You can't do anything for that. If a notification is fired from the server and application is in background then the alert will be displayed.
I have done a lot of search in past for this stuff.
I have done this in one of my work. this is what i did.
when application enter : - (void)applicationDidEnterBackground:(UIApplication *)application
I send data to server using ASIHTTPRequest with property :
[request setShouldContinueWhenAppEntersBackground:YES];
But after finished, i didn't do anymore connection or data manipulation. So, only the connection is running at background and not your app. you can't do much after the connection finish.
As #rckoenes was mentioned, you may not execute task too long.
If you would like to update server data while your app is running in the background mode, the application should be active at that time. It can only be active if it uses "music, or voip, or location tracking", otherwise the app will be paused in background mode.
One way to avoid this is to develop your application, and to set it to use, for example, «location tracking». This will allow it to meet the requirements for active background process and you will be able to update server data.
Unfortunately, I do not know whether the app can pass app store approval with this set-up.
However, if you are interested in this solution, you can find an example here.

Start data downloading in background mode

I need to have the following things to work while my iphone application is in the background mode.
1.Run a clock.
2.Communicate with the server every 15 mins to get the server time and one another value.
3.Need to start downloading data in background mode.
I searched a lot whether these are possible or not. Kindly give confirmation on these.
I am developing an iPhone application which involves Ticket Booking System. I registered my application as location based beacuse it is using user's location taken in background for a purpose.
My problem is that i need to run an internal clock in my application in background mode. I need to write the codes for internal clock in core location delegate methods, so that internal clock will also run along with the location bsed services. Will my app get rejected? Is anything wrong in doing like this?
I need to get the correct time to use in my app, so that i am running this internal clock. I can use NSDate, but that will return the device time. Anyone can change the device time. So once somebody chaged, wrong time will affect the smooth functioning of the app. Kindly some body suggest to get the correct time with out running the internal clock ?
I suppose you want to do this with your app not running in the foreground - and that is not possible, if you don't use some tricks like playing an empty audio file and pretending to be a music player or the like.
In iOS, you can only execute code of your app while it is actively running in the foreground, except for some specific tasks like VOIP or music playing.
If you want to do this while your app is running in the foreground, just use NSTimer and a background process for loading, like it was suggested. But then you should also prevent the iPhone from entering SLEEP mode after 1 minute, otherwise it won't work when th euser is not actively using the app during the 15 minutes ...
This is possible, what you need to do is,
1] Run a background thread, in which set a NSTimer with 15mins.
2] Set repeat:YES to call it at every 15mins.
3] And start download your need there!
I will do like this if I stuck like your situation!

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.