Reachability类:
1.这个类用于检测当前网络状态,它不是SDK的一部分,可以在iOS Developer Library里找到这份代码。
从苹果网站上下载Reachability.zip文件,解压之。
2.重用Reachability类
(1)把Reachability.h和Reachability.m文件拖到项目中。
(2)添加框架:SystemConfiguration.framework。
3.同步的Reachability
(1)使用同步的方式是比较简单,导入Reachability.h头文件,然后通过代码检查网络:
#import “Reachability.h”
。。。some code omitted…
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus status = [reach currentReachabilityStatus];
(2)通过检查某个主机能否访问来判断当前网络是否可用:
Reachability *reach = [Reachability reachabilityWithHostName:@“www.apple.com”];
NetworkStatus status = [reach currentReachabilityStatus];
(3)案例:
创建一个工程,并添加Reachability.h和Reachability.m到工程中,并链接SystemConfiguration.framework.
在AppDelegate.h头文件中导入Reachability.h,并添加一个实例方法。
#import <UIKit/UIKit.h>
#import "Reachability.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
- (NSString *)stringFromStatus:(NetworkStatus)status;
@end
在AppDelegate.m中这样实现:
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
Reachability *reach = [Reachability reachabilityForInternetConnection];
NetworkStatus status = [reach currentReachabilityStatus];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Reachability" message:[self stringFromStatus:status] delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
return YES;
}
- (NSString *)stringFromStatus:(NetworkStatus)status{
NSString *string;
switch (status) {
case NotReachable:
string = @"网络故障";
break;
case ReachableViaWiFi:
string = @"wifi链接";
break;
case ReachableViaWWAN:
string = @"wwan";
break;
default:
string = @"unknown";
break;
}
return string;
}
4.异步的Reachability
(1)异步的方式稍微复杂,不过通过这种方式可以来订阅实时的网络状态变化通知。导入Reachability.h头文件,然后注册一个对象来订阅网络状态变化的信息,网络状态变化的信息名称为kReachabilityChanged-Notification.如下:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
(2)你需要创建一个Reachability对象实例并开始向外发布网络状态变化的消息:
//这里的reach必须是strong类型的成员引用,切记!
self.reach = [Reachability reachabilityWithHostName:@“www.apple.com”];
[reach startNotifier];
(3)当网络状态发生变化的时候,Reachability对象将调用reachabilityChanged:方法,可以在这个方法里面获取当前的网络状态,然后做相应的处理。
- (void)reachabilityChanged:(NSNotification *)notification{
Reachability *reach = [notification object];
if([reach isKindOfClass:[Reachability class]]){
NetworkStatus status = [reach currentReachabilityStatus];
//Insert your code here
}
}
(4)例子:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
self.hostReach = [Reachability reachabilityWithHostName:@"www.19500.com"];
[self.hostReach startNotifier];
NSLog(@"how are you");
return YES;
}
- (void)reachabilityChanged:(NSNotification*)note
{
NSLog(@"haha");
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass:[Reachability class]]);
NetworkStatus status = [curReach currentReachabilityStatus];
if (status == NotReachable) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"19500彩票网"
message:@"没有网络连接,请检查网络"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
alert.tag = 9080;
alert.delegate = self;
[alert show];
}
}
5.原生 Reachability API
前面将的Reachability类实际上是苹果公司对SCNetworkReachability API的封装,这个API定义在SystemConfigure.framework库中。如果有其他特别的需求,也可以直接使用这个原生的SCNetworkReachability类。