Is background tasks the solution for this problem? - iphone

I need to develop an enterprise app that monitors the network traffic. Basically it detects if the user is in wi-fi or cellular data and save the amount of bytes was sent and received in a period of time.
I saw an App at the AppStore that do exactly this job.
Detecting wi-fi or cellular data is quite simple using the Reachability Sample provided by Apple.
My problem is to keep monitoring the bytes sent and received while the app is in background.
As it is an enterprise App, I used UIBackgroundModes "voip" to avoid the app to be terminated.
I also installed the setKeepAliveTimeout method and I'm able to see the logs each 10 minutes, BUT only for 10 seconds after the method runs. I mean, setKeepAliveTimeout brings my App to run a Timer for 10 seconds each 1o minutes.
I'm thinking wether or not a task in background is the best solution for my problem.
I'll appreciate any comments.
EDIT: Ok guys. Thats the perfect way to do it.
First of all you must read this:
http://www.christian-fries.de/blog/files/tag-ios.html
I tried this and it works really fine.
All we need to do is to create a second thread detached from the main one. This way we have a continuos threading running forever. You must see the GCD docs at Apple's website also.
Second thing you should consider for an enterprise App is to set it up as a voip App, this way iOS will put your App running even after a reboot. It's a special behavior iOS has to keep voip Apps running.
Thats it guys.
I hope it can help you.

We dont have access to this data. There is no way to measure traffic.
The app you saw made an estimation (eg. 1mb/min) and after applicationWillEnterForeground. then you calc time x estimated-traffic and there we go.
Unfortunately there is no way to measure,
so I did a traffic-reset in settings and then i streamed exactly 1 min music. after a few repetitions I had my results. The problem is, that this works only fine with static traffic-processes like audio or video.
hope could help
endo
EDIT: look answer below!

Related

How to start a App automatically in swift

I am wondering if you know of a way in which n App can receive a messages/file or even "wakeup" while it is not active initially.
Please let me know if there is a mechanism or even if there is a path worth exploring.
It's not a mechanism and I never tested it but according to Apples Developer Library if your App is monitoring some area (up to 20 geofencing areas per App) and if you will enter that area, the Application will be relaunched, at least AppDelegate will be called even if the app is terminated.
If a region boundary crossing occurs while your iOS app is not
running, the system automatically wakes it up (or relaunches it) in
the background so that it can process the event.
Read here. Maybe this could help you a little. I'd love to see your finding on that topic. :)
I have some fancy idea about this. You could update the geofencing areas each time you will enter one. So you will have an App that will relaunch it self over an over if you have good network connection and are mowing around. :D

Rejected App, Watchdog timer or missing entitlement

So I have an app that has twice been rejected from the app store and I really could use some help!
It has been rejected for the Watchdog timer forcing it to quit when it is first opened. However I have not been able to reproduce this on any of my devices. It does take a long time to build to a device when using xcode (i know that when built from xcode the watchdog is disconnected). But I have a couple questions that might help me fix this.
Does the watchdog take into account only didFinishLaunchingWithOptions: or are their other methods that it calls?
Does the number of frameworks used by the program have anything to do with it? and if so is 11 frameworks just too many?
Once the app is installed to a device, and I unplug said device is the watchdog timer re-enabled?
What are some other things that can add to the watchdog timer?
Apple said that another possibility could be Another possibility could be a missing entitlement. Could someone please explain what this means, I hav'nt seen this before with my other apps but thought maybe it was something new? And does it have to be installed on all apps for the appstore?
Time Profile for first seconds
You are blocking the UI thread for too long. To solve this problem, first you'll need to figure out what code took that long. It might be worthwhile to profile your app. Note that the simulator doesn't emulate the device, it only emulates the API. It could run much faster than the actual device because your computer is more powerful than an iPhone. Try click and hold Run at top left of Xcode, and choose Profile. Choose Time Profiler and run for a few seconds. Stop the profiler to analyze timing in function calls. Note that you may need to dig deeper, find some tutorials or books to really understand profiling.
When you know what parts have been running slow. You can put them into a thread. The easy way would be to use Grand Central Dispatch. To get started, visit this tutorial. You can skip the first half and focus on the actual thread blocks. The APIs are dispatch_queue_create, dispatch_async and dispatch_release.
This will unblock your UI.
Do you do a lot of processing in
application:didFinishLaunchingWithOptions,
applicationDidBecomeActive,
applicationDidEnterBackground,
applicationWillResignActive or
applicationWillTerminate?
Things like multiple file access, loading/saving data, synchronous network access can cause your application to freeze on startup or when the user exits, and could cause the watchdog to kill your app.
This Apple Technical note suggests that the watchdog is only looking at the launch, suspend, resume and terminate user interface events, so I would say you should look into these first, and maybe post back some more details of what your app does in the methods listed above.
http://developer.apple.com/library/ios/#qa/qa1693/_index.html
11 frameworks is fine. I have used 11 in an app no problem.
If you start the app up by tapping the icon on the home screen then the watchdog timers will be active and monitoring your app. If you start the app up by hitting run or debug in Xcode, then the watchdog will not be monitoring your app.

iPhone battery life suggestions

I am developing a voice recording application that communicates with the server real-time, therefore requiring persistent Internet connectivity. I have included UIRequiresPersistentWiFi in my info.plist and have also disabled device going to sleep when the app is active. However, this understandably has a serious effect on the battery life of the device. Users end up having to leave this continuously plugged in. For an app that is touted as meant to replace handheld recorders, this is a serious shortcoming. My app also has many xml threads sent from iPhone to server, so wireless connectivity is of paramount importance.
My questions are:
1. Is it possible to somehow switch on UIRequiresPersistentWifi status only when required during a session. For example, can this be switched on only during transfers or xml updates to server and be switched off at other times?
2. Likewise, can IdleTimer be enabled and disabled programmatically at will during a session. For example, enabling idletimer only when no foreground or background tasks are running on the device.
Any other suggestion to ensure normal battery life? All my users will be on iTouch 4 or iPhone 4.
Any help/suggestions would be greatly appreciated.
Answer to part 2: Yes, you can enable and disable the idle timer depending on what the user or app is doing, and how long it's been. I know of a couple apps that disable the idle timer, but then re-enable it if the user doesn't touch any UI elements for 10 minutes, but then re-disables it if the user starts some long operation again. etc.
Partial answer to part 1: Using the radios (sending wifi data) takes power. A good way to save power is to not send data for as long as possible. Maybe buffer large amounts of data on the device, and try to burst upload it later.
My suggestion would be to not communicate with the server continuously, sorry :(
Is it possible to cache chunks of data into a file on the phone and transmit the chunks to the server periodically in one big burst? Same for the XML. Or does your app really really require it to be broadcast real-time?
And as far as I know, if you have specified UIRequiresPersistentWifi, you're stuck with it :(
Sorry, probably not the answer you want!

Continuous Timeline in Iphone App?

Background: Workout App is designed to have a continuous timeline that I can then input what exercise and reps I'm doing in real-time, then graph the results in real-time.
Issue: When the iphone sleeps for too long, we lose the continuity of the Timeline and it restarts. We haven't figured out how to keep the timeline updating while the App is sleeping. I've asked him to do a call to the Iphone internal clock and do the math on where the viewer should be and move them to that point on the timeline. However, he can't figure out how to do this.
Solutions: Have you folks heard of anyways to do this call to the internal clock? Perhaps a better question, can you think of another way around the problem in general?
You could try CACurrentMediaTime which returns mach_absolute_time in seconds. This should be accurate across sleep.
There is no certain way that can assure you app will be running in the background for ever. (If this is what you need)
Even Steve said is Multitasking, iPhone resources are very limited and this means that your app will might be killed while it is in the background without any kind of notification.
(Except by the notification you get when entering the background)
The best thing you can do is low your resource usage. (release as many objects as possible)
Because when the system needs memory it will start killing apps in the background starting by the app that uses more resources.
If you are implementing streaming protocols then is different.
Hope this helps

Does iOS 4 make “Real Multitasking” available to 3rd party developers?

Ever since the first beta came out I’ve been trying to find out if “real” multitasking is possible — i.e. can you put a program in the background and have it hang on to a network connection indefinitely?
I’m thinking about IM or IRC apps, for example. I’ve compiled an app myself on iOS 4, and without changing a thing it appeared to stay running in the background, but for all I know it was just suspended to memory.
The docs say the best you can do is request up to 10 minutes, but in the developer presentation they showed off Skype sitting in the background and then notifying the user that a call was coming in. Does anyone know for sure how this all works?
It appears the answer is no. The API for Skype is a very special case, called the "voip" mode, and requires special behavior, such as marking the socket in use for VoIP.
You can receive alarm notifications in the background (such as time passed). The amount of time you are in the background running state is severely limited by the OS.
Android's background model is complete and in many ways much nicer.
Apple has a guide named "Supporting Multitasking In Your Applications" which you should be able to locate.
Apple's iOS 4 developer docs outline this all very clearly.
When your app is closed or switched away from, it is almost immediately "suspended", meaning the OS freezes the app's state. When the user switches back to your app, your code keeps running just where it kept off. You don't need to add any code to your app to do this, just compile it against OS 4.
The above is true in most cases. There are two reasons the "suspended" model may not apply:
1) If the device starts to run low on memory, the OS will start terminating suspended apps that haven't been switched to in a while, without warning. This is why it's in your best interest for your app to remember it's state as well, so if your app is terminated, then re-opened, the user doesn't really notice because it still returns to right where they left off.
2) Your app uses one of the "background" APIs. These are for audio playback, VoIP services, or location services. In this case, your app is allowed to continue running in the background but only has access to those APIs. Additionally, your app can designate certain long-running tasks as "background tasks" that need to be completed before the app is suspended or terminated, like uploading pictures to Flickr or rendering a video, etc.
The "background task" method doesn't cover pinging servers indefinitely, as there is a time limit for the task, after which it will be forcibly halted. Apps that need that sort of functionality are expected to implement push notifications, just as before.
That should clear this up. All in all I think it's a pretty elegant solution to multitasking on a mobile device.
iOS 4 applications can either be running or suspended. The operating system will try to keep as many requested applications as possible in memory, while all other applications are suspended.
Applications that run in the background can access features such as navigation, audio, and VOIP (but NOT instant messaging). So it looks like you might be out of luck.
-- PC World Multitasking on Apples iPhone 4
It is possible for apps to request background time. Read the docs. I would say it iOS is "controlled multitasking".
You can create a long running background task, I believe these can include networking features. Just have to set the background task flag on the work block.
https://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
the OS can limit exactly how much time you get though... it will tell you when your time is up giving you a chance to cleanup nicely.
iOS 4 has "real" multitasking to some extend. There are two things to consider:
The UI event loop is single tasking. So only the front app executes on the UI event loop. Not a big deal: detach your main code form the UI event loop (maybe you need to redesign your app).
The OS "may" terminate your app if some criteria are met (e.g. low memory).
Currently one of these criteria is that execution time is limited to 10 minutes (real time not cpu time). However I expect this to change and see better criteria for background app termination (I hope to).
Apart from this you can have timers (event loops) in background.
There is no real multitasking in iOS 4.2 even. because apps will only be allowed to finish the task related to states..for small interval of time and then it will be in suspended state.. If you will set background task for long interval of time then... it will behave unexpectedly like no method will be called when you will try to run the app from anywhere..
You may be interested in this blog post that outlines how "multitasking" works in systems such as iPhone OS 4 and Android.
in fact u can do this, although it's not allowed by Apple. u gotta set up a toolchain in ur mac and use some unofficial SDK...
check http://code.google.com/p/iphone-backgrounder/ for more information
You should use the Push Notifications framework for the feature set you are creating!