使用ios自带sample判断网络连接

开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息。如果没有处理它们,是不会通过Apple的审查的。

Reachability

Apple 的 例程 Reachability 中介绍了取得/检测网络状态的方法。在你的程序中使用 Reachability 只须将该例程中的 Reachability.h 和 Reachability.m 拷贝到你的工程中。如下图:

Reachability

然后将 SystemConfiguration.framework 添加进工程:

Reachability

Reachability 中全局定义了3种网络状态,定义了一个消息名字(在未来的消费class中也会通过这个检测消息):

typedefenum {

NotReachable =0,

ReachableViaWiFi,

ReachableViaWWAN

} NetworkStatus;

#define kReachabilityChangedNotification @"kNetworkReachabilityChangedNotification"


高级网络判断

//WWAN may be available, but not active until a connection has been established.

//WiFi may require a connection for VPN on Demand.

- (BOOL) connectionRequired;

//

  1. static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) 
  2.     #pragma unused (target, flags) 
  3.     NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback"); 
  4.     NSCAssert([(NSObject*) info isKindOfClass: [Reachability class]], @"info was wrong class in ReachabilityCallback"); 
  5.  
  6.     //We're on the main RunLoop, so an NSAutoreleasePool is not necessary, but is added defensively 
  7.     // in case someon uses the Reachablity object in a different thread. 
  8.     NSAutoreleasePool* myPool = [[NSAutoreleasePool alloc] init]; 
  9.      
  10.     Reachability* noteObject = (Reachability*) info; 
  11.     // 
  12.     // Post a notification to notify the client that the network reachability changed. 
  13.     [[NSNotificationCenter <span style="color: rgb(255, 102, 102);">defaultCenter</span>] postNotificationName: kReachabilityChangedNotification object: noteObject]; 
  14.     //可以充分使用这 
  15.     [myPool release]; 
  16.  
  17. - (BOOL) startNotifier 
  18.     BOOL retVal = NO; 
  19.     SCNetworkReachabilityContext    context = {0, self, NULL, NULL, NULL}; 
  20.     //TRUE if the notification client was successfully set。。。。 
  21.     if(SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context)) 
  22.     { //特定的runloop 
  23.         if(SCNetworkReachabilityScheduleWithRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)) 
  24.         { 
  25.             retVal = YES; 
  26.         } 
  27.     } 
  28.     return retVal; 
  29.  
  30. - (void) stopNotifier 
  31.     if(reachabilityRef!= NULL) 
  32.     { 
  33.         SCNetworkReachabilityUnscheduleFromRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 
  34.     } 
static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
{
	#pragma unused (target, flags)
	NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
	NSCAssert([(NSObject*) info isKindOfClass: [Reachability class]], @"info was wrong class in ReachabilityCallback");

	//We're on the main RunLoop, so an NSAutoreleasePool is not necessary, but is added defensively
	// in case someon uses the Reachablity object in a different thread.
	NSAutoreleasePool* myPool = [[NSAutoreleasePool alloc] init];
	
	Reachability* noteObject = (Reachability*) info;
    //
	// Post a notification to notify the client that the network reachability changed.
	[[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject];
	//可以充分使用这
	[myPool release];
}

- (BOOL) startNotifier
{
	BOOL retVal = NO;
	SCNetworkReachabilityContext	context = {0, self, NULL, NULL, NULL};
    //TRUE if the notification client was successfully set。。。。
	if(SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context))
	{ //特定的runloop
		if(SCNetworkReachabilityScheduleWithRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode))
		{
			retVal = YES;
		}
	}
	return retVal;
}

- (void) stopNotifier
{
	if(reachabilityRef!= NULL)
	{
		SCNetworkReachabilityUnscheduleFromRunLoop(reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
	}
}

可以看到示范代码考虑得非常细,通过callback然后postnotification;使用

[[NSNotificationCenterdefaultCenter] addObserver:self selector: @selector(reachabilityChanged:)name: kReachabilityChangedNotificationobject: nil];


Here is a checklist for integrating an observer for the connection:

  1. Get the Reachability class from Apple, it has all the functionality we need to check if a connection is up/down
  2. Import the class into your app
  3. Initialize it and create an observer
  4. Create a method that handles an alteration of the network status


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值