我已经检查了实现阿什利米尔的可达性类没有可可荚/依赖管理器。其思想是使项目中的可达性依赖项免费。
Xcode 7.2 - Swift 2.1
1) https://github.com/ashleymills/Reachability.swift。下载将Reachability类添加到项目中。
注:添加时,请确保勾选“如需复制项目”。
2)创建一个AppManager.swift类。这个类将迎合公共模型类,其中公共方法和数据将被添加,可以在任何VC中使用。
// AppManager.swift
import UIKit
import Foundation
class AppManager: NSObject{
var delegate:AppManagerDelegate? = nil
private var _useClosures:Bool = false
private var reachability: Reachability?
private var _isReachability:Bool = false
private var _reachabiltyNetworkType :String?
var isReachability:Bool {
get {return _isReachability}
}
var reachabiltyNetworkType:String {
get {return _reachabiltyNetworkType! }
}
// Create a shared instance of AppManager
final class var sharedInstance : AppManager {
struct Static {
static var instance : AppManager?
}
if !(Static.instance != nil) {
Static.instance = AppManager()
}
return Static.instance!
}
// Reachability Methods
func initRechabilityMonitor() {
print("initialize rechability...")
do {
let reachability = try Reachability.reachabilityForInternetConnection()
self.reachability = reachability
} catch ReachabilityError.FailedToCreateWithAddress(let address) {
print("Unable to create\nReachability with address:\n\(address)")
return
} catch {}
if (_useClosures) {
reachability?.whenReachable = { reachability in
self.notifyReachability(reachability)
}
reachability?.whenUnreachable = { reachability in
self.notifyReachability(reachability)
}
} else {
self.notifyReachability(reachability!)
}
do {
try reachability?.startNotifier()
} catch {
print("unable to start notifier")
return
}
}
private func notifyReachability(reachability:Reachability) {
if reachability.isReachable() {
self._isReachability = true
//Determine Network Type
if reachability.isReachableViaWiFi() {
self._reachabiltyNetworkType = CONNECTION_NETWORK_TYPE.WIFI_NETWORK.rawValue
} else {
self._reachabiltyNetworkType = CONNECTION_NETWORK_TYPE.WWAN_NETWORK.rawValue
}
} else {
self._isReachability = false
self._reachabiltyNetworkType = CONNECTION_NETWORK_TYPE.OTHER.rawValue
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
}
func reachabilityChanged(note: NSNotification) {
let reachability = note.object as! Reachability
dispatch_async(dispatch_get_main_queue()) {
if (self._useClosures) {
self.reachability?.whenReachable = { reachability in
self.notifyReachability(reachability)
}
self.reachability?.whenUnreachable = { reachability in
self.notifyReachability(reachability)
}
} else {
self.notifyReachability(reachability)
}
self.delegate?.reachabilityStatusChangeHandler(reachability)
}
}
deinit {
reachability?.stopNotifier()
if (!_useClosures) {
NSNotificationCenter.defaultCenter().removeObserver(self, name: ReachabilityChangedNotification, object: nil)
}
}
}
3)创建一个委托类。我使用委托方法来通知连接状态。
// Protocols.swift
import Foundation
@objc protocol AppManagerDelegate:NSObjectProtocol {
func reachabilityStatusChangeHandler(reachability:Reachability)
}
4)创建UIViewController的父类(继承方法)。父类具有所有子vc都可以访问的方法。
// UIappViewController.swift
import UIKit
class UIappViewController: UIViewController,AppManagerDelegate {
var manager:AppManager = AppManager.sharedInstance
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func reachabilityStatusChangeHandler(reachability: Reachability) {
if reachability.isReachable() {
print("isReachable")
} else {
print("notReachable")
}
}
}
5)在AppDelegate中启动实时Internet连接监控。
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
AppManager.sharedInstance.initRechabilityMonitor()
return true
}
6)我已经添加了一个Swift文件名AppReference来存储常量enum值。
// AppReference.swift
import Foundation
enum CONNECTION_NETWORK_TYPE : String {
case WIFI_NETWORK = "Wifi"
case WWAN_NETWORK = "Cellular"
case OTHER = "Other"
}
7)在ViewController上(例如,你只想在网络可用的情况下调用API)
// ViewController.swift
import UIKit
class ViewController: UIappViewController {
var reachability:Reachability?
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
if(AppManager.sharedInstance.isReachability)
{
print("net available")
//call API from here.
} else {
dispatch_async(dispatch_get_main_queue()) {
print("net not available")
//Show Alert
}
}
//Determine Network Type
if(AppManager.sharedInstance.reachabiltyNetworkType == "Wifi")
{
print(".Wifi")
}
else if (AppManager.sharedInstance.reachabiltyNetworkType == "Cellular")
{
print(".Cellular")
}
else {
dispatch_async(dispatch_get_main_queue()) {
print("Network not reachable")
}
}
}
override func viewWillAppear(animated: Bool) {
}
override func didReceiveMemoryWarning() {
}
}
样本可以在https://github.com/alvinreuben/Reachability-Sample上下载
升级到Swift 3.1-
https://github.com/alvinvgeorge/Reachability-UpgradedToSwift3