osx - Cocoa: determine network availability on wake -
i'm writing cocoa os x application needs perform different actions depending on whether or not there internet connectivity when system wakes sleep. use notification center of nsworkspace.sharedworkspace()
receive wake notifications so:
nsworkspace.sharedworkspace().notificationcenter.addobserver(self, selector: "onsystemwake:", name: nsworkspacedidwakenotification, object: nil)
my problem implementation of onsystemwake:
. use reachability sample code query if there internet connectivity when system wakes:
@objc func onsystemwake(notif: nsnotification) { let status = internetreachability.currentreachabilitystatus() if status.value == notreachable.value { // internet not reachable, stuff. } else { // internet reachable, other stuff. } }
this seems work fine when i'm @ home , put laptop sleep , wake again. however, if laptop switches known wifi during sleep (e.g. when arrive @ university), code above detects there no internet reachability when laptop wakes sleep @ new location - despite there known wifi available.
i assume cause of network connection still in process of being established when system wake notification received application.
i delay internet reachability check occur x seconds after system wake notification has been received, seems bad solution combinations of laptops , networks may have longer connection times others.
is there way notified when network adapters (both wired , wireless) have finished connection attempts?
it better claim network it! because maybe connected network network! not connected internet!
for internet reachability use code below , works fine me on ios!
let domain: nsurl = nsurl(string : "http://google.com/")! let req : nsmutableurlrequest = nsmutableurlrequest(url: domain) req.httpmethod = "post" let data = "\0" req.httpbody = data.datausingencoding(nsutf8stringencoding) nsurlconnection.sendasynchronousrequest(req, queue: nsoperationqueue.mainqueue()) { (respons,data,error) in print(respons) if respons == nil { \\ internet not active!! } else { \\ internet active!! } }
the above code saying google means internet! if can reach google.com means i'm connected network anyway! if not i'm not!
the problem if using network company network , network isn't providing internet connection! above code won't satisfy needs! in case , of course depend on network have request else! router!
Comments
Post a Comment