IOS background work - iphone

I have an iPhone application like facebook for iPhone. My application must connect my server and read all message every two hours regularly. I have a thread to read all message but when the application is terminated the thread cannot work. Can the thread run undependently from main delegate or how can I find solution for this problem?

You cannot have your app do stuff in the background. There is an API to finish tasks like uploading a photo but even that will be killed after around 10 minutes.
But the Apple Push Notification Service seems like the most appropriate solution for your problem. Your server notifies the device that there is something new happening and you fetch the actual messages when the user opens the app.
edit: As of iOS 7 Apple implemented a feature where you can schedule running tasks to fetch data in the background. Those tasks are not guaranteed to run at any specific times. See the release notes for iOS 7 and the linked methods below:
Apps that regularly update their content by contacting a server can
register with the system and be launched periodically to retrieve that
content in the background. To register, include the UIBackgroundModes
key with the fetch value in your app’s Info.plist file. Then, when
your app is launched, call the setMinimumBackgroundFetchInterval:
method to determine how often it receives update messages. Finally,
you must also implement the
application:performFetchWithCompletionHandler: method in your app
delegate.

There is no solution.
Apple does not permit applications to run in the background unless they are of a specific type such as location or audio or voip or newstand (your app can continue to run for about 10 minutes after it was active if it uses shouldBeginBackgroundTaskWithExpirationHandler).
There is no workaround, many many other people have wondered how to do the same thing as yourself before, but there is no legitimate way. Your app cannot schedule any sort of periodic call home activity.
The only way your app can run once its gone into a suspended or terminated state is for the user to launch it, either explicitly or in reponse to a local notification or remote push notification.

Related

Keep infomed for Device location will Application in Backgroud

I'am developing an application that keep a WebService informed for device location each 5minute (for exemple) :
So when the application leave the foreground execution and enter background I have to lunch a timer who
1 - Update the device location
2 - Send the location to a web service
How can I perform this action ? Or do you know any code exemple that I can follow to achieve this design ?
Thank you for your help !
Doesn't look like anyone gave you a solid answer, so allow me.
There is no iOS approved way of running a 5 minute (or any minute) timer in the background (unless your app is VOIP or music). What you CAN do is register your app as requiring location services in the background (edit the info.plist and add a key Required Background Modes and then add a value App registers for location updates. What this means is your locationManager:didUpdateToLocation:fromLocation: method and corresponding region monitoring/SLC methods will fire on location change, but Timers will not work at all.
The reason why is that timers require a run loop (a thread executing code) to be running and must piggy-back on that thread, but when the app is in the background even when executing code from the LocationManager, the run loop that executes the code almost always finishes before your timer would go off.
Hope this helps!
you cannot do this (on iOS).
(unless your app requests permissions that are meant for Navigation apps)
Hello #Aladdin Gallas,
I have developed a simple application that supports background location updates for iOS and Android in Xamarin.
The app pushes a new location every 2 seconds (you can change it to 5 minutes if you want to).
There is a feature I haven't been able to add; it is to keep the service up when the user closes the app (for iPhone). Other than that, as long as the app is open, even if the phone is locked, the service keeps running.
You can take a look at the application as a reference if it helps.
GitHub Repo

Launch Application from background with internet Availability

Is it possible to get a notification (that launches the app) when the network gets available?What I want is my application to update its contents as soon the internet gets available.The only way I can think of is to send a push from the server once is a while (may be once a day)and that tell the user it might be a good idea to get yourself synchronized.
This is currently impossible if your background task scope is limited to that feature. The only way you could do this is if you had some other legitimate reason to become a background app (aka VOIP or GPS) and run reachability checks every X minutes, and then post a UILocalNotification that allows the user optionally open the app (you cannot programmatically open your app yourself).

How to invoke a method daily at a specific time in iPhone application?

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.

How to run a ~30sec process in the background every hour (iphone app)

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.

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.