Created
May 10, 2017 20:28
-
-
Save agnosticdev/b2c7af87fc90adb1fe87e0e203634d51 to your computer and use it in GitHub Desktop.
AlamoFire NetworkReachabilityManager Test For Device Running 10.3.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Alamofire | |
import UIKit | |
class MasterViewController: UITableViewController { | |
@IBOutlet weak var titleImageView: UIImageView! | |
var detailViewController: DetailViewController? = nil | |
var objects = NSMutableArray() | |
var networkReachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.apple.com") | |
var onlineStatus: Bool = false | |
var timer: Timer! | |
// MARK: - View Lifecycle | |
override func awakeFromNib() { | |
super.awakeFromNib() | |
navigationItem.titleView = titleImageView | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
if let split = splitViewController { | |
let controllers = split.viewControllers | |
if | |
let navigationController = controllers.last as? UINavigationController, | |
let topViewController = navigationController.topViewController as? DetailViewController | |
{ | |
detailViewController = topViewController | |
} | |
} | |
setupReachabilityManager() | |
startNetworkTimer() | |
} | |
override public func viewWillDisappear(_ animated: Bool) { | |
super.viewWillDisappear(animated) | |
stopNetworkTimer() | |
} | |
// MARK: - Instance Methods | |
func setupReachabilityManager() { | |
networkReachabilityManager?.listener = { [unowned self] status in | |
switch status { | |
case .notReachable: | |
self.onlineStatus = false | |
case .reachable(_), .unknown: | |
self.onlineStatus = true | |
} | |
} | |
networkReachabilityManager?.startListening() | |
} | |
func startNetworkTimer() { | |
timer = Timer.scheduledTimer(timeInterval: 1.0, | |
target: self, | |
selector: #selector(networkTimerCallback), | |
userInfo: nil, | |
repeats: true) | |
} | |
func stopNetworkTimer() { | |
timer.invalidate() | |
} | |
@objc public final func networkTimerCallback() { | |
print("Online Status: \(self.onlineStatus)") | |
} | |
// MARK: - UIStoryboardSegue | |
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { | |
if | |
let navigationController = segue.destination as? UINavigationController, | |
let detailViewController = navigationController.topViewController as? DetailViewController | |
{ | |
func requestForSegue(_ segue: UIStoryboardSegue) -> Request? { | |
switch segue.identifier! { | |
case "GET": | |
detailViewController.segueIdentifier = "GET" | |
return Alamofire.request("https://httpbin.org/get") | |
case "POST": | |
detailViewController.segueIdentifier = "POST" | |
return Alamofire.request("https://httpbin.org/post", method: .post) | |
case "PUT": | |
detailViewController.segueIdentifier = "PUT" | |
return Alamofire.request("https://httpbin.org/put", method: .put) | |
case "DELETE": | |
detailViewController.segueIdentifier = "DELETE" | |
return Alamofire.request("https://httpbin.org/delete", method: .delete) | |
case "DOWNLOAD": | |
detailViewController.segueIdentifier = "DOWNLOAD" | |
let destination = DownloadRequest.suggestedDownloadDestination( | |
for: .cachesDirectory, | |
in: .userDomainMask | |
) | |
return Alamofire.download("https://httpbin.org/stream/1", to: destination) | |
default: | |
return nil | |
} | |
} | |
if let request = requestForSegue(segue) { | |
detailViewController.request = request | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment