(1)定义网络监测实体
#import "Reachability.h"
// Add this to the interface in the .m file of your view controller
@interface MyViewController (){
Reachability *internetReachableFoo;
}
@end
(2)具体调用在视图控制层.m文件实现
// 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];
}
Demo: https://github.com/tonymillion/Reachability