Swift - 添加网络检查Reachability

两篇关于Reachability的使用方法
Reachability使用
how to use Reachability

github 下载地址

上代码:


// 检查检查网络状态是否当前网络可用
    func checkNetworkStates() {
        // 初始化
        self.reach = Reachability.reachabilityForInternetConnection()
        self.reach?.reachableOnWWAN = false
        // 添加通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(AppDelegate.reachabilityChanged(_:)), name: kReachabilityChangedNotification, object: nil)
        self.reach!.startNotifier()
    }
    
// 每次有网络切换的时候调用
    func reachabilityChanged(noti:NSNotification) {
        if self.reach!.isReachableViaWiFi() || self.reach!.isReachableViaWWAN() {
            print("当前网络可用")
        }else {
            print("当前网络不可用")
  // 网络不可用是添加 提示框
           altertStatus()
            
        }
    }

// 无网络状态添加提示框
    func altertStatus() {
        let alerContl = UIAlertController.init(title: "斗笔提示您:", message: "无网络链接", preferredStyle: UIAlertControllerStyle.Alert)
        let action1 = UIAlertAction.init(title: "取消", style: UIAlertActionStyle.Cancel, handler: nil)
        let action2 = UIAlertAction.init(title: "设置", style: UIAlertActionStyle.Default) { (action) in
// 打开系统wifi 设置界面
            let url = NSURL(string: "prefs:root=WIFI")
            UIApplication.sharedApplication().openURL(url!)
        }
        alerContl.addAction(action1)
        alerContl.addAction(action2)
        self.window?.rootViewController?.presentViewController(alerContl, animated: true, completion: nil)
    }```

如何打开系统的各种设置界面:
#####首先,进行设置info 的URL Types
![截图](http://upload-images.jianshu.io/upload_images/1285286-c5b5387d348c1ddc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
```swift
 let url = NSURL(string: "prefs:root=WIFI")
 UIApplication.sharedApplication().openURL(url!)

所有设置对应的url:

Usage — prefs:root=General&path=USAGE  

VPN — prefs:root=General&path=Network/VPN  

Wallpaper — prefs:root=Wallpaper  

Wi-Fi — prefs:root=WIFI

Setting—prefs:root=INTERNET_TETHERING

Storage & Backup — prefs:root=CASTLE&path=STORAGE_AND_BACKUP  

International — prefs:root=General&path=INTERNATIONAL  

Location Services — prefs:root=LOCATION_SERVICES  

Music — prefs:root=MUSIC  

Music Equalizer — prefs:root=MUSIC&path=EQ  

Music VolumeLimit— prefs:root=MUSIC&path=VolumeLimit  

Network — prefs:root=General&path=Network  

Nike + iPod — prefs:root=NIKE_PLUS_IPOD  

Notes — prefs:root=NOTES  

About — prefs:root=General&path=About  

Accessibility — prefs:root=General&path=ACCESSIBILITY  

AirplaneModeOn— prefs:root=AIRPLANE_MODE  

Auto-Lock — prefs:root=General&path=AUTOLOCK  

Brightness — prefs:root=Brightness  

Bluetooth — prefs:root=General&path=Bluetooth

Date& Time — prefs:root=General&path=DATE_AND_TIME  

FaceTime — prefs:root=FACETIME

General— prefs:root=General

Keyboard — prefs:root=General&path=Keyboard  

iCloud — prefs:root=CASTLE  iCloud 

Notification — prefs:root=NOTIFICATIONS_ID  

Phone — prefs:root=Phone  

Photos — prefs:root=Photos  

Profile — prefs:root=General&path=ManagedConfigurationList  

Reset — prefs:root=General&path=Reset  

Safari — prefs:root=Safari  Siri — prefs:root=General&path=Assistant  

Sounds — prefs:root=Sounds  

SoftwareUpdate— prefs:root=General&path=SOFTWARE_UPDATE_LINK  

Store — prefs:root=STORE  

Twitter — prefs:root=TWITTER  

这里参考文章:iOS 跳转到系统的设置界面

你可能感兴趣的:(Swift - 添加网络检查Reachability)