ios - Detect Internet Connection and display UIAlertview Swift 3 -


i making app detects if there connection internet using if else statement, when there internet, nothing if there no internet connection, alert view app requires internet managed found reachability implement on viewdidload() uialertview seems not working. using this:

public class reachability { class func isconnectedtonetwork() -> bool {     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(memorylayout.size(ofvalue: zeroaddress))     zeroaddress.sin_family = sa_family_t(af_inet)      let defaultroutereachability = withunsafepointer(to: &zeroaddress) {         $0.withmemoryrebound(to: sockaddr.self, capacity: 1) {zerosockaddress in             scnetworkreachabilitycreatewithaddress(nil, zerosockaddress)         }     }     var flags: scnetworkreachabilityflags = scnetworkreachabilityflags(rawvalue: 0)     if scnetworkreachabilitygetflags(defaultroutereachability!, &flags) == false {         return false     }     let isreachable = flags == .reachable     let needsconnection = flags == .connectionrequired     return isreachable && !needsconnection     } } 

then in view controller class:

override func viewdidload() {         if reachability.isconnectedtonetwork() == true         {             print("connected")         }         else         {             let controller = uialertcontroller(title: "no internet detected", message: "this app requires internet connection", preferredstyle: .alert)             let ok = uialertaction(title: "ok", style: .default, handler: nil)             let cancel = uialertaction(title: "cancel", style: .cancel, handler: nil)              controller.addaction(ok)             controller.addaction(cancel)              present(controller, animated: true, completion: nil)         }         super.viewdidload()         //...     } 

and looks alertview doesn't popup suggestion? or help? thanks

you have put alertcontroller in viewdidappear method of viewcontrollers lifecycle, because in methods viewdidload und viewwillappear current view still not in window hierachy.
if want present further viewcontrollers alertcontroller need full loaded view in new viewcontroller rendered:

override func viewdidload() {     super.viewdidload()     //viewcontrollers view not in window hierarchy     // here make further initialization of views subviews }  override func viewwillappear(_ animated: bool) {     //viewcontrollers view ist still not in window hierarchy     //this right place instance animations on views subviews }  override func viewdidappear(_ animated: bool) {     // viewcontrollers view ist loaded , present further viewcontroller     //here other ui operations     if reachability.isconnectedtonetwork() == true     {         print("connected")     }     else     {         let controller = uialertcontroller(title: "no internet detected", message: "this app requires internet connection", preferredstyle: .alert)         let ok = uialertaction(title: "ok", style: .default, handler: nil)         let cancel = uialertaction(title: "cancel", style: .cancel, handler: nil)          controller.addaction(ok)         controller.addaction(cancel)          present(controller, animated: true, completion: nil)     }  } 

this apple documentation saying viewcontroller: uiviewcontroller methods called follows:

  • viewdidload()—called when view controller’s content view (the top of view hierarchy) created , loaded storyboard. method intended initial setup. however, because views may purged due limited resources in app, there no guarantee called once.
  • viewwillappear()—intended operations want occur before view becomes visible. because view’s visibility may toggled or obscured other views, method called before content view appears onscreen.
  • viewdidappear()—intended operations want occur view becomes visible, such fetching data or showing animation. because view’s visibility may toggled or obscured other views, method called after content view appears onscreen.

reference: https://developer.apple.com/library/content/referencelibrary/gettingstarted/developiosappsswift/lesson4.html


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -