ios - Alamofire -What to do when there is a wifi connection but no internet connection? -
i'm using alamofire in ios app. use bool values in viewwillappear , in appdelegate nsnotifications check if there internet connection. if there no wifi connection pop appears inform user. if there wifi connection pop disappears , works fine again. i've had no problems long wifi not working.
i @ meetup , explained me way works it looks wifi connection , not internet connection. e.g.. if have wifi router , it's plugged in router isn't connected internet alamofire view successful connection because connecting wifi although doesn't know wifi can't connect internet.
i in situation connected open network, app responded if connected internet (no pop up) couldn't connect anything. wifi signal on full. in terminal ran ping , turns out connection dead. app couldn't tell difference.
how make pop appear in sitaution this?
also .case unknown for?
alamofire.swift:
import foundation import alamofire open class networkmanager { open static var sharedmanager: networkreachabilitymanager = { let reachabilitymanager = networkreachabilitymanager() reachabilitymanager?.listener = { (status) in switch status { case .notreachable: print("the network not reachable") notificationcenter.default.post(name: nsnotification.name(rawvalue: "unsuccessful"), object: nil) case .unknown : //??????? print("it unknown wether network reachable") //i'm not sure whether put notification successful or unsuccessful??? case .reachable(.ethernetorwifi): print("the network reachable on wifi connection") notificationcenter.default.post(name: nsnotification.name(rawvalue: "successful"), object: nil) case .reachable(.wwan): print("the network reachable on wwan connection") notificationcenter.default.post(name: nsnotification.name(rawvalue: "successful"), object: nil) } } reachabilitymanager?.startlistening() return reachabilitymanager! }() }
appdelegate:
func application(_ application: uiapplication, didfinishlaunchingwithoptions launchoptions: [uiapplicationlaunchoptionskey: any]?) -> bool { networkmanager.sharedmanager.startlistening()
somevc:
override func viewwillappear() { super.viewwillappear() nsnotificationcenter.defaultcenter().addobserver(self, selector: #selector(successful), name: "successful", object: nil) nsnotificationcenter.defaultcenter().addobserver(self, selector: #selector(unsuccessful), name: "unsuccessful", object: nil) if networkmanager.sharedmanager.isreachable == true{ self.successful() }else{ self.unsuccessful() } if networkmanager.sharedmanager.isreachableonwwan == true{ self.successful() }else{ self.unsuccessful() } if networkmanager.sharedmanager.isreachableonethernetorwifi == true{ self.successful() }else{ self.unsuccessful() } } func successful(){ //dismiss pop } func unsuccessful(){ //show pop } deinit{ nsnotificationcenter.defaultcenter().removeobserver(self, name: "successful", object: nil) nsnotificationcenter.defaultcenter().removeobserver(self, name: "unsuccessful", object: nil) } }
you can init networkreachabilitymanager
with host, example, google host, because default 0.0.0.0
let reachabilitymanager = alamofire.networkreachabilitymanager(host: "www.google.com")
when start listening reachability manager doing ping host. if network available can cache ssid , ping again when ssid changed.
for case .unknown
better put notification unsuccessful.
example ssid (it doesn't work in simulator):
func fetchssidinfo() -> string? { if let interfaces = cncopysupportedinterfaces() { in 0..<cfarraygetcount(interfaces){ let interfacename: unsaferawpointer = cfarraygetvalueatindex(interfaces, i) let rec = unsafebitcast(interfacename, to: anyobject.self) let unsafeinterfacedata = cncopycurrentnetworkinfo("\(rec)" cfstring) if let unsafeinterfacedata = unsafeinterfacedata as? dictionary<anyhashable, any> { return unsafeinterfacedata["ssid"] as? string } } } return nil }
Comments
Post a Comment