Some favorite site feeds aggregated locally: iPhone Development RSS   Adobe Labs RSS   Macrumors RSS

Wednesday, May 13, 2009

iPhone: Detect online network status

Wednesday, May 13, 2009   

For my previous post I showed you how you can cache in memory images from URLs... however, what if the device is not able to connect to a network? The previous example will hang and eventually bomb back to the springboard because it's not coded to handle that condition.

For me this network detection thing was a bit of a Holy Grail item. I'd seen a few examples around from Apple that seemed like they used tons of code to get the job done (or maybe I just didn't understand their code well enough yet). Then I read online about a seismic XML tutorial from Apple that did network detection.

I popped open the project in the Xcode Documentation window and started checking out the .h and .m files. I saw the implementation they used, and it didn't involve tons of code. And after testing it, it works just fine for the moment.

I've since been told that I could use a separate thread and populate the initial UIImageViews with a stock image, and the threaded process would fetch and place the artwork as needed (to allow for initial smooth scrolling). I don't know enough about that yet though, so this will have to do.

Make sure you Add the SystemConfiguration.framework to your project, and include it in your .m (#import ).

In your .h file, define a BOOL, I called mine "availableNetwork" - you don't need to assign it as a property, just up in the interface block. Make sure you also define the method in the .h... -(BOOL)isDataSourceAvailable;

Here are the two methods I'm using to report back network status (in my .m):
- (BOOL)isDataSourceAvailable {

static BOOL checkNetwork = YES;
static BOOL _isDataSourceAvailable = NO;
if (checkNetwork) { // Since checking the reachability of a host can be expensive, cache the result and perform the reachability check once.
checkNetwork = NO;
Boolean success;
const char *host_name = "google.com"; //pretty reliable :)
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host_name);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags);
_isDataSourceAvailable = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
CFRelease(reachability);
}
return _isDataSourceAvailable;
}
That's the method we'll call when we load our view, and set our "availableNetwork" value to. loadView is triggered before viewDidLoad.
- (void)loadView {

availableNetwork = NO;
BOOL returnVal = [self isDataSourceAvailable];
availableNetwork = returnVal;
...
And there you have it. It's a one shot deal here, if connectivity returns, the flag won't be reset. You could always run a NSTimer that checks every now and then, or I am sure there is some kind of notification of that kind of change one could listen for... I'm not far enough along with all of this to know yet.

Labels: , ,

 
 Return to the main page
Comments:

There are currently 6 Comments:

Anonymous Mark Tyler said...
“Thanks Eric for making such an easy to follow tutorial. I was able to implement it into my iPhone application in just a few minutes. It works wonderfully and I again thank you for providing this to the development community.”
 
Blogger e.dolecki said...
July 17, 2009 12:24 PM
“Mark,

I'm glad that I could help. It's post like this one around the net that I learn from. The more of these out there, be better applications we're likely to see out in the wild.

Thanks.”
 
Anonymous Anonymous said...
November 1, 2009 5:31 PM
“hey
I want to write a client prog for iPhone where user taps a button and the Label of the button gets communicated via WiFi to a server ( Mac/Winodws)
Can it be done ?
If yes can u post me some help ?
Thanks

Rahul (rahulgupta.87@gmail.com)”
 
Blogger e.dolecki said...
November 2, 2009 12:24 PM
“Anon - yes this could be done. use sender id to get the button instance, read the label property of it, and send it to a server using a http thang. Google is your best friend.”
 
Blogger mona said...
March 4, 2010 6:04 AM
“hi
i am trying to write a client/server app for the iphone and was wondering if there is a method from inside the application the application code to detect and connect to a wireless network
any help is greatly appreciated”
 
Blogger e.dolecki said...
March 4, 2010 7:18 AM
“mona,

The above knows connected status (wifi or edge) - isn't that all you need to know? Or do you need to connect to a specific wifi network (name/type)?

The iPhone does connections on it's own too you know...”
 
 Leave a comment