Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How App in background can check for internet connection, IOS?

My Iphone app have 2 mode, online mode, and offline mode. if user is using app in online mode, the request gets submitted to server immediately. if user is using app in offline mode, i will store the request locally, so that i can upload it when internet is available. I am using reachability class to check internet availability. Reachability is working fine with app. I am not able to achieve following scenario. I had stored request as user is working in offline mode, mean while user will push application in background by clicking home button in iPhone. after some time user will activate the wifi or cellular connection in his device. when user enables internet connection, my app which is in background should get notified and should start submitting request to server. How can i achieve this functionality? Many ios app may be having same this requirement. how they are doing it? any idea?

I tried following step to achieve this, but wasn't successful.what I am i doing wrong here, help me.

1) I added observer for kReachabilityChangedNotification inside applicationDidEnterBackground delegate method. but my app fails to listen to the Kreachabiliychagednotification, in this method.

2) If i add observer for kReachabilityChangedNotification inside applicationdidbecomeActive, then my app is able to listen for Kreachabiliychagednotification.

Why IOS doesnt allow to listen for Kreachabiliychagednotification when app is in background?

like image 883
RockandRoll Avatar asked Aug 07 '13 06:08

RockandRoll


2 Answers

The answer is that you can't unless you application will have one of those specific use (quoting Apple doc):

Support for some types of background execution must be declared in advance by the app that uses them. An app declares support for a service using its Info.plist file. Add the UIBackgroundModes key to your Info.plist file and set its value to an array containing one or more of the following strings:

*audio—The app plays audible content to the user while in the background. (This content includes streaming audio or video content using AirPlay.)
*location—The app keeps users informed of their location, even while it is running in the background.
*voip—The app provides the ability for the user to make phone calls using an Internet connection.
*newsstand-content—The app is a Newsstand app that downloads and processes magazine or newspaper content in the background.
*external-accessory—The app works with a hardware accessory that needs to deliver updates on a regular schedule through the External Accessory framework.
*bluetooth-central—The app works with a Bluetooth accessory that needs to deliver updates on a regular schedule through the Core Bluetooth framework.
*bluetooth-peripheral—The app supports Bluetooth communication in peripheral mode through the Core Bluetooth framework.

You can only check for reachability again re-entering foreground.

like image 86
Andrea Avatar answered Oct 02 '22 05:10

Andrea


i came to handle the same scenario when i was on a project but i came to know that By default in the background state app stays for a short time only, most apps move to the suspended state shortly afterward. That mean the app is in the background but is not executing code. So your custom implemented notification do not work. Must requery NetworkReachability at Wakeup Time in app delegate methodes:

applicationWillEnterForeground:
applicationDidBecomeActive 

this was the best i did to handle that problem, hope it helps

like image 27
Hemant_Negi Avatar answered Oct 02 '22 05:10

Hemant_Negi