popup alert when Reachability connection is lost during using the app (IOS xcode swift) -


i beginner of ios app development , "popup alert when reachability connection lost during using app (ios xcode swift)", popup alert when starting app. there not alert popup during using app when internet connection lost. please kindly help, thanks!

what did: 1) creat reachability.swift file , worte

import foundation  public class reachability {  class func isconnectedtonetwork()->bool{      var status:bool = false      let url = nsurl(string: "http://google.com/")      let request = nsmutableurlrequest(url: url!)      request.httpmethod = "head"     request.cachepolicy = nsurlrequestcachepolicy.reloadignoringlocalandremotecachedata     request.timeoutinterval = 10.0              var response: nsurlresponse?      var data = nsurlconnection.sendsynchronousrequest(request, returningresponse: &response, error: nil) nsdata?      if let httpresponse = response as? nshttpurlresponse {          if httpresponse.statuscode == 200 {             status = true         }     }      return status  }  } 

2) edit viewcontroller.swift file below

import uikit class viewcontroller: uiviewcontroller {  @iboutlet weak var webview: uiwebview!  //viewdidload method override func viewdidload() {     super.viewdidload()      if reachability.isconnectedtonetwork() == true {         println("internet connection ok")     } else {         println("internet connection failed")         var alert = uialertview(title: "no internet connection",                                 message: "make sure device connected internet.",                                  delegate: nil,                                  cancelbuttontitle: "ok")          alert.show()      }     var url = nsurl(string: "http://www.google.com/")      webview.loadrequest(nsurlrequest(url: url!)) }  override func didreceivememorywarning() {     super.didreceivememorywarning() }  } 

try reachabilty class, add in project , following in viewcontroller

let reachability = reachability.reachabilityforinternetconnection()  reachability.whenreachable = { reachability in if reachability.isreachableviawifi() {  let alertcontroller = uialertcontroller(title: "alert", message: "reachable via wifi", preferredstyle: .alert)      let defaultaction = uialertaction(title: "ok", style: .default, handler: nil)     alertcontroller.addaction(defaultaction)      presentviewcontroller(alertcontroller, animated: true, completion: nil)  }  else { let alertcontroller = uialertcontroller(title: "alert", message: "reachable via cellular", preferredstyle: .alert)      let defaultaction = uialertaction(title: "ok", style: .default, handler: nil)     alertcontroller.addaction(defaultaction)      presentviewcontroller(alertcontroller, animated: true, completion: nil)     }   } reachability.whenunreachable = { reachability in let alertcontroller = uialertcontroller(title: "alert", message: "not reachable", preferredstyle: .alert)      let defaultaction = uialertaction(title: "ok", style: .default, handler: nil)     alertcontroller.addaction(defaultaction)      presentviewcontroller(alertcontroller, animated: true, completion: nil) }  reachability.startnotifier() 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -