怎么使用reachability demo查询联网状态

Reachability(判断网络是否连接) - pengyingh - 博客园
类似于一个网络状况的探针。
  [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];&& &&&& hostReach = [[Reachability reachabilityWithHostName: @""] retain];&& &[hostReach startNotifier];&& &&&& wifiReach=[[Reachability reachabilityForLocalWiFi]retain];&&& [wifiReach startNotifier];
- (void)reachabilityChanged: (NSNotification* )note {&&& Reachability* curReach = [note object];&&& NSParameterAssert([curReach isKindOfClass: [Reachability class]]);&& &&&& NetworkStatus netStatus = [curReach currentReachabilityStatus];&& &&&& switch (netStatus)&&& {&&&&&&& case NotReachable:&&&&&&& {&&&&&&&&&&& NSLog(@"Access Not Available");&&&&&&&&&&&&&&&&&& }&&&&&&&&&& &&&&&&&& case ReachableViaWWAN:&&&&&&& {&&&&&&&&&&& NSLog(@"Reachable WWAN");&&&&&&&&&&&&&&&&&& }&&&&&&& case ReachableViaWiFi:&&&&&&& {&&&&&&&&&&& NSLog(@"Reachable WiFi");&&&&&&&&&&&&&&&&&& }&&& }}
判断网络是否连接
/**** 此函数用来判断是否网络连接服务器正常* 需要导入Reachability类*/+ (BOOL)isExistenceNetwork{
BOOL isExistenceN
Reachability *reachability = [Reachability reachabilityWithHostName:@""];
// 测试服务器状态
switch([reachability currentReachabilityStatus]) {
case NotReachable:
isExistenceNetwork = FALSE;
case ReachableViaWWAN:
isExistenceNetwork = TRUE;
case ReachableViaWiFi:
isExistenceNetwork = TRUE;
isExistenceN}
实时通知网络状况
/** 在应用委托的方法didFinishLaunchingWithOptions中添加*/[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];reachability = [[Reachability reachabilityWithHostName:@""] retain];[reachability startNotifier];........return YES;/***此函数通过判断联网方式,通知给用户*/- (void)reachabilityChanged:(NSNotification *)notification{
Reachability *curReachability = [notification object];
NSParameterAssert([curReachability isKindOfClass:[Reachability class]]);
NetworkStatus curStatus = [curReachability currentReachabilityStatus];
if(curStatus == NotReachable) {
[DOIN_Util logFax:@"连接失败"];5529人阅读
&&&&& 这个类可以用来检测用户是否连接到internet.& 用法非常简单,只有一个方法,返回 YES或NO。
一个简单的例子:
if ([Connection isConnected]) {
这个类的头文件:
Connection.h
#import &Foundation/Foundation.h&
#import &SystemConfiguration/SystemConfiguration.h&
#import &netinet/in.h&
#import &arpa/inet.h&
#import &netdb.h&
@interface Connection : NSObject {
+ (BOOL) isC
类的实现文件:
Connection.m
#import "Connection.h"
@implementation Connection
+ (BOOL) isConnected {
// Create zero addy
struct sockaddr_in zeroA
bzero(&zeroAddress, sizeof(zeroAddress));
zeroAddress.sin_len = sizeof(zeroAddress);
zeroAddress.sin_family = AF_INET;
// Recover reachability flags
SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
SCNetworkReachabilityF
BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
CFRelease(defaultRouteReachability);
if (!didRetrieveFlags) {
NSLog(@"Error. Could not recover network reachability flags");
return NO;
BOOL isReachable = flags & kSCNetworkFlagsR
BOOL needsConnection = flags & kSCNetworkFlagsConnectionR
BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientC
NSURL *testURL = [NSURL URLWithString:@"/"];
NSURLRequest *testRequest = [NSURLRequest requestWithURL:testURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0];
NSURLConnection *testConnection = [[[NSURLConnection alloc] initWithRequest:testRequest delegate:self] autorelease];
return ((isReachable && !needsConnection) || nonWiFi) ? (testConnection ? YES : NO) : NO;
最后别忘了添加必要的frameworks: SystemConfiguration and libz.1.1.3.dylib
标题中提到的Reachability也具有检测网络的功能。大家可以参考资料:
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:988481次
积分:14030
积分:14030
排名:第273名
原创:365篇
转载:45篇
译文:10篇
评论:414条Reachability&2.0使用
我本地保存的ASIHttp库中 新增加的方法就能实现这一效果
可以参看官方demo
一:确认网络环境3G/WIFI
1. 添加源文件和framework
开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息。如果没有处理它们,是不会通过Apple的审查的。
Apple 的 例程 Reachability
中介绍了取得/检测网络状态的方法。要在应用程序程序中使用Reachability,首先要完成如下两部:
1.1. 添加源文件:
在你的程序中使用 Reachability 只须将该例程中的 Reachability.h 和 Reachability.m
拷贝到你的工程中。如下图:
1.2.添加framework:
将SystemConfiguration.framework 添加进工程。如下图:
2. 网络状态
Reachability.h中定义了三种网络状态:
typedef enum {
NotReachable = 0, //无连接
ReachableViaWiFi, //使用3G/GPRS网络
ReachableViaWWAN //使用WiFi网络
} NetworkS
因此可以这样检查网络状态:
Reachability *r = [Reachability
reachabilityWithHostName:@“”];
switch ([r currentReachabilityStatus]) {
case NotReachable:
// 没有网络连接
case ReachableViaWWAN:
// 使用3G网络
case ReachableViaWiFi:
// 使用WiFi网络
& & Reachability* hostR
& & Reachability*
& & Reachability* wifiR
// 监听网络状态是否改变的通知 其中kReachabilityChangedNotification
在Reachability的h文件中定义 相应的方法是reachabilityChanged:
[[NSNotificationCenter defaultCenter] addObserver: self selector:
@selector(reachabilityChanged:) name:
kReachabilityChangedNotification object: nil];
hostReach = [[Reachability reachabilityWithHostName:
@""] retain];// 检查ping某个主机
& & // 开启定时检测网络状态功能
[hostReach startNotifier];
[self updateInterfaceWithReachability: hostReach];
& & internetReach =
[[Reachability reachabilityForInternetConnection] retain];
[internetReach startNotifier];// 检查TCP/IP Routing(没明白
官方demo这么显示的)
[self updateInterfaceWithReachability: internetReach];
& & wifiReach = [[Reachability
reachabilityForLocalWiFi] retain];
[wifiReach startNotifier];// 检查Local Wifi
[self updateInterfaceWithReachability: wifiReach];
- (void) reachabilityChanged: (NSNotification* )note
Reachability* curReach = [note object];
// 没有网络时处理
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

我要回帖

更多关于 reachability 的文章

 

随机推荐