* iOS7 see note at the end of this post first!
When you have an enterprise app, used for production follow up or something, were being online is crucial, connectivity loss or even a short fall out can bring frustrations and loss of data.
Using web services, the app will keep on working when there is no connection, simply throws errors when trying to fetch data or save data. To make the app more responsive, I build in a simple connectivity check so only offline functionality will be available.
To check if there is a WIFI or 3G connection, you can use the Reachability class given by Apple. I attached the file to this post:
Reachability.h (3.63 kb)
Reachability.m (9.26 kb)
You can also find it here: https://developer.apple.com/Library/ios/samplecode/Reachability/Introduction/Intro.html
Now once you added the Reachability.h and .m file to your project, you can simply use this code to check for connectivity:
Add this variable to your view controller:
Then in the view load initialise it and add a notifier to it, so your app is notified when there is a change in the connectivity:
reachability = [ReachabilityreachabilityForInternetConnection];
[[NSNotificationCenterdefaultCenter] addObserver:selfselector:@selector(reachabilityDidChange:) name:kReachabilityChangedNotificationobject:reachability];
Now when the connectivity changes, the reachabilityDidChange is called:
- (void)reachabilityDidChange:(NSNotification *)notification
NetworkStatus status = [reachabilitycurrentReachabilityStatus];
if(status == NotReachable)
self.colVwDashBoard.backgroundColor = [UIColorredColor];
else if (status == ReachableViaWiFi || status == ReachableViaWWAN)
self.colVwDashBoard.backgroundColor = [UIColorcolorWithRed:204.0/255.0green:204.0/255.0blue:204.0/255.0alpha:1];
What I'm doing, is check if there is WIFI or 3G and if not, I will make the collection view background red, you could disable all functionality that needs connection.
In fact thats all there is. Easy right?
After upgrading to iOS 7 on some devices, this implementation was not properly working, so I switched to AFNetworking class, I will post the easy implementation soon on this blog, I'll try to find time to work out a small and clear example.
When connection is lost, background turns red and only the camera icon will respond to clicks:
When connection is back, the background turns gray again: