iPhone data transmission issue - iphone

I have an iPhone app that uses ASIHTTPRequest to transmit data input by the user to a remote MySQL database via a PHP webservice layer. This works perfectly.
If the user presses the submit button the data should be sent regardless...the problem arises when there is insufficient bandwidth...rather than displaying some uialert to inform the user, I would like to implement some kind of function that constantly 'sniffs' for an internet connection even when the app isn't running (in main view) that ensures that the user only has to press 'submit' once.
How is this possible? Has anyone come across any tutorials/examples of anything similar?

Check out this example application from Apple: Reachability
It'll help you with some code to detect when the connection has changed.
Here's a link about backgrounding tasks. As you'll read, you can request additional time to complete a task, but it won't wait an infinite amount of time until it's complete. Background Tasks

Use the Reachability API in conjunction with a flag of some sort that will perform the desired action once it detects that a connection is available.

Related

iOS chat-app: How do I notify the user that the app will not function anymore if the user leaves the app?

I'm developing a chat-app for iOS that must use an existing server API. The way it works is pretty straightforward: the app checks every given interval whether there are new messages on the server and displays them, plus, it sends the server new messages that the user typed.
When the user starts 'multi-tasking' or presses the home button, my app will go to the background and therefore will not be able to check the server for new messages. The server will automatically assume that the user stopped the chat when a certain timeout has been reached.
Often, the user isn't aware of the fact that when the app is put to the background, it is unable to maintain the connection to the server and will stop the chat. I'm looking for a method that will notify the user of this behavior as soon as the app is put to the background.
My current idea is to notify the user when applicationWillResignActive and/or applicationDidEnterBackground is fired, but I wouldn't know in what way. Can it be done in a way that complies with Apple's guidelines?
I'm aware of the fact that the best solution would be a different overall design of the software (e.g., using push notifications and no server-side chat termination by timeouts), but in this case I can't change that.
I would continue running in the background and set an expiration handler block (called by the os when you app is REALLY killed) and there schedule a UILocalNotification
use
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler

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.

do I need to continually check the connection on every button press in my iphone app?

I am building a video streaming app that allows users to create playlists and has content that updates quite regularly. It has 4 main views, each with a list of videos, the content of which is loaded as xml when the view is loaded and cached for a period of time (not when the app is loaded)
So basically at any time on any view the app requires a valid internet connection.
I have set the 'Application uses wifi' to YES in the plist.
I have tested the reachability example code as seen in this excellent answer How to check for an active Internet connection on iOS or OSX? and added it to each of my views viewDidLoad methods.
My question is how often and where should I implement this? am I right in including it in each views viewDidLoad or should I create some other class that I can call on more often?
Many thanks
You do not need to check continually the connection. You must check just before initiating an operation that requires a working connection. So, you do not check in viewDidLoad, or in another class.
You are in charge of reporting to your users that the connection is not available if this is the case. Before starting an operation requiring the connection to the Internet, you do the check. If the connection is not available notify immediately the users.
It may happen that the connection is available when you do the check but becomes unavailable later, during the operation. You must be careful here to include in your code a check for this. When you lose the connection, again you need to notify the users.

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.

iPhone - how to be notified of call completion

I'm developing an application that needs to take action on completed phone calls, preferably right after the call ends but minimally once per day.
I've read up on the new CoreTelphony framework, and it seems I can get call events if my app is active, but I don't see how to launch/wake my app when a call ends if my app is not the foreground app. I also don't see how any of the new pseudo-background "modes" would allow my app to listen for these events in the background. Do any of you know how this might be done?
If post-call processing isn't possible, then I'd like to figure out a way to automatically wake my app up once per day, pull all of the call events since the last wakeup, and process them. I know how I might do this with Push or Local notifications, but my understanding is that those require user action to continue; in this case, I just want the processing to happen automatically. Is there a mechanism that would enable this?
Thanks,
Dan
You can't launch your app without user interaction.
Push / Local notifications aren't for this kind of thing, they're for letting the user know about event.
On a non-jailbroken device there is no way to do what you want to do.