一:确认网络环境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网络
} NetworkStatus;
因此可以这样检查网络状态:
Reachability *r = [Reachability reachabilityWithHostName
switch ([r currentReachabilityStatu
case NotReachable:
// 没有网络连接
break;
case ReachableViaWWAN:
// 使用3G网络
break;
case ReachableViaWiFi:
// 使用WiFi网络
break;
}
// 监听网络状态是否改变的通知 其中kReachabilityChangedNoti
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNoti
hostReach = [[Reachability reachabilityWithHostName
[hostReach startNotifier];
[self updateInterfaceWithReach
[internetReach startNotifier];// 检查TCP/IP Routing(没明白 官方demo这么显示的)
[self updateInterfaceWithReach
[wifiReach startNotifier];// 检查Local Wifi
[self updateInterfaceWithReach
- (void) reachabilityChanged: (NSNotification* )note
{
Reachability* curReach = [note object];
// 没有网络时处理
}