The best way to detect any change to Internet Connection xcode swift -


i wondering if there best way detect change happens internet connection

i mean using code

import systemconfiguration var isconnected: bool = false func isconnectedtonetwork() {  var zeroaddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0)) zeroaddress.sin_len = uint8(sizeofvalue(zeroaddress)) zeroaddress.sin_family = sa_family_t(af_inet)  let defaultroutereachability = withunsafepointer(&zeroaddress) {     scnetworkreachabilitycreatewithaddress(nil, unsafepointer($0)).takeretainedvalue() }  var flags: scnetworkreachabilityflags = 0 if scnetworkreachabilitygetflags(defaultroutereachability, &flags) == 0 {     self.isconnected = false }  let isreachable = (flags & uint32(kscnetworkflagsreachable)) != 0 let needsconnection = (flags & uint32(kscnetworkflagsconnectionrequired)) != 0  if isreachable && !needsconnection {   self.isconnected = true }  else{      self.isconnected = false    }       } 

it returns status internet connection, though need keep checking internet connection while app running used code

nstimer.scheduledtimerwithtimeinterval(10, target: self, selector: selector("isconnectedtonetwork"), userinfo: nil, repeats: true) 

each 10 seconds checks internet connection, believe there better way. mean using observer or else.

such 1

nsnotificationcenter.defaultcenter().addobserver(self, selector: "keyboarddidshow", name: uikeyboarddidshownotification, object: nil) 

when keyboard shows detects , calls function keyboarddidshow()

swift 3

this clean solution managed reach.

you need install pod https://github.com/afnetworking/afnetworking

then import afnetworking declare networkreachability class.

protocol networkreachabilitydelegate {     var networkreachability: networkreachability { set }     func reachabilitychanged(status: networkreachabilitystatus) }  enum networkreachabilitystatus {     case unknown     case notreachable     case reachable }  class networkreachability {     private var previousnetworkreachabilitystatus: networkreachabilitystatus = .unknown     var delegate: networkreachabilitydelegate?  func startmonitoring() {     afnetworkreachabilitymanager.shared().startmonitoring()     afnetworkreachabilitymanager.shared().setreachabilitystatuschange { status in         // var declared don't need import afnetworking everytime conform networkreachabilitydelegate         var networkstatus: networkreachabilitystatus          switch status {         case .unknown:             networkstatus = .unknown         case .notreachable:             networkstatus = .notreachable         case .reachableviawifi, .reachableviawwan:             networkstatus = .reachable         }          if (self.previousnetworkreachabilitystatus != .unknown && networkstatus != self.previousnetworkreachabilitystatus) {             self.delegate?.reachabilitychanged(status: networkstatus)         }         self.previousnetworkreachabilitystatus = networkstatus     } }  func stopmonitoring() {     afnetworkreachabilitymanager.shared().stopmonitoring() } } 
  1. conform networkreachabilitydelegate want monitor network status changes.
  2. set networkreachability = networkreachability()
  3. set networkreachability.delegate = self
  4. receive callbacks on reachabilitychanged(status: networkreachabilitystatus) function.

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -