I'm building an app that needs to make HTTP requests to an API. I want to handle the network connection status , so when the network is not reachable it presents a popup dialog and when the network is reachable again the popup dismisses.
Currently I am able to do that , THE PROBLEM is that when the popup is dismissed because the network is reachable again I do not know how to (or where to) make the HTTP requests again.
I have build a singleton's class to handle reachability:
import Foundation
import PopupDialog
import Alamofire
class Reachability {
private var currentViewController : UIViewController? = nil
private var popup : PopupDialog? = nil
let title = "Network connection error".localized
let message = "It seems you have problems with connection, please check your internet connection.".localized
let settingsButton = DefaultButton(title: "Settings".localized, action: {
if let url = URL(string:"App-Prefs:root=Settings&path=General") {
UIApplication.shared.open(url)
}
})
//shared instance
static let shared = Reachability()
let reachabilityManager = Alamofire.NetworkReachabilityManager(host: "www.google.com")
}
extension Reachability{
/**
Starts network observer and manages to listen on different kind of network
changes. Popup warning dialogs will be presented based on different kind of network status
such as network is not reachable and once network resorts back online defined popup dialog will
be dismissed automatically.
**/
public func startNetworkReachabilityObserver() {
if let status = reachabilityManager?.isReachable, status == false {
if self.currentViewController != nil{
}
self.presentReachabilityPopup()
return
}
reachabilityManager?.listener = { status in
switch status {
case .notReachable:
if self.currentViewController != nil {
self.presentReachabilityPopup()
}
break
case .unknown :
break
case .reachable(.ethernetOrWiFi), .reachable(.wwan):
if self.popup != nil ,self.currentViewController != nil {
self.popup?.dismiss()
}
break
}
}
reachabilityManager?.startListening()
}
public func stopNetworkReachabilityObserver() {
reachabilityManager?.stopListening()
}
// Configure current view controller as a network observer
public func currentViewController(_ vc: UIViewController?) {
self.currentViewController = vc
self.startNetworkReachabilityObserver() // Start network observer
}
// Presents an alert dialog box notifying the users concerning the network issues
private func presentReachabilityPopup() {
self.popup = PopupDialog(title: title, message: message )
self.popup?.addButton(settingsButton)
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 3.5) {
if self.currentViewController != nil{
self.currentViewController?.present(self.popup!, animated: true, completion: nil)
}
}
}
}
And here is an example of a ViewController that needs to make the HTTP request when the network is reachable again:
import UIKit
import GoogleMaps
class ExploreViewController: UIViewController, GMSMapViewDelegate{
//MARK: Properties
var posts:[Post] = []
var followers:[User] = []
var images = [UIImage]()
@IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Explore".localized
// Configure reachability observer on this controller
Reachability.shared.currentViewController(self)
if Location.shared.requestAuth(){
self.fetchPosts()
self.setUpCameraPosition()
}
}
The function fetchPosts() contains the HTTP requests I need to perform.
You can set a flag when the https api network connection is started for the first time.
var networkFlag = true
In that case your program would know that you actually started a network call. Next, when you are in between the request and internet connectivity is not reachable, it would give a popup.
Solution to trigger https call again: In code, where you check if network Reachability is true, put an if condition, and check the flag you set earlier if network connection was made.
//check for network reachability
if(reachable)
{
//check the flag set earlier which indicates network call was made
if(networkFlag)
{
//make an https call--your network call
//https:call put your method call here
}
Hope this helps :)
Screenshots:
Internet connection OK
Reopening app. Internet connection: WRONG ( Airplane mode )
Internet connection OK again but not fetching
Here is what I have modified:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Explore".localized
// Configure reachability observer on this controller
Reachability.shared.currentViewController(self)
if Location.shared.requestAuth(){
if (Reachability.shared.reachabilityManager?.isReachable)!{
if self.networkFlag{
self.fetchPosts()
self.setUpCameraPosition()
}
}else{
self.networkFlag = false
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.