iOS 常用第三方库7(Reachability)

Reachability 是一个检测网络状态的框架。
开发Web等网络应用程序的时候,需要确认网络环境,连接情况等信息。如果没有处理这些信息的话,是不会通过Apple的审查的。

使用方法:

将Reachability.h 和 Reachability.m 拷贝到你的工程中。

然后:

var reach:Reachability?
func checkNetStatus(){
self.reach = Reachability.reachabilityForInternetConnection()
// Tell the reachability that we DON'T want to be reachable on 3G/EDGE/CDMA
self.reach!.reachableOnWWAN = false
if reach?.currentReachabilityStatus() == NetworkStatus.ReachableViaWWAN {
self.net = NetState.Mobile
}
// Here we set up a NSNotification observer. The Reachability that caused the notification
// is passed in the object parameter
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "reachabilityChanged:",
name: kReachabilityChangedNotification,
object: nil)
self.reach!.startNotifier()
}
func reachabilityChanged(notification: NSNotification){
if self.reach!.isReachableViaWiFi() {
print("能访问网络")
}else if self.reach!.isReachableViaWWAN(){
appDelegate.isCanUseNetWork = true
print("正在使用移动网络")
}else {
print("没有网络!!!")
self.net = NetState.None
}
}