-
Detect if an Internet Connection exists in iPhone using (ARC and GCD compatible) class
over 9 years ago
over 9 years ago
Hi Readers,
Below are steps you need to follow when you are checking if their is an Active Internet connection in an Iphone using (ARC and GCD compatible) class.
1) Add SystemConfiguration framework to the project but dont worry about including it anywhere
2) Add T.Millions version of Reachability.h and Reachability.m to the project.
3) Update the interface section:
- #import "Reachability.h"
- // Add this to the interface in the .m file of your view controller
- @interface MyViewController ()
- {
- Reachability *internetReachableFoo;
- }
- @end
#import "Reachability.h" // Add this to the interface in the .m file of your view controller @interface MyViewController () { Reachability *internetReachableFoo; } @end
4) Then implement this method in the .m file of your view controller which you can call:
- **// Checks if we have an internet connection or not
- - (void)testInternetConnection
- {
- internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"];
- // Internet is reachable
- internetReachableFoo.reachableBlock = ^(Reachability*reach)
- {
- // Update the UI on the main thread
- dispatch_async(dispatch_get_main_queue(), ^{
- NSLog(@"Yayyy, we have the interwebs!");
- });
- };
- // Internet is not reachable
- internetReachableFoo.unreachableBlock = ^(Reachability*reach)
- {
- // Update the UI on the main thread
- dispatch_async(dispatch_get_main_queue(), ^{
- NSLog(@"Someone broke the internet :(");
- });
- };
- [internetReachableFoo startNotifier];
- }
**// Checks if we have an internet connection or not - (void)testInternetConnection { internetReachableFoo = [Reachability reachabilityWithHostname:@"www.google.com"]; // Internet is reachable internetReachableFoo.reachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Yayyy, we have the interwebs!"); }); }; // Internet is not reachable internetReachableFoo.unreachableBlock = ^(Reachability*reach) { // Update the UI on the main thread dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Someone broke the internet :("); }); }; [internetReachableFoo startNotifier]; }
0 Comment(s)