How check internet connection on one place to whole app in swift

Note : not require to check internet connection in all view controller just add code on launch app.

        import Network
        
        let monitor = NWPathMonitor()
        
        let queue = DispatchQueue(label: "NetStatus_Monitor")
        monitor.start(queue: queue)
        
        monitor.pathUpdateHandler = { path in
            if path.status == .satisfied {
                
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
                self.InternetConnectionAlert("Warnning", "We are connected to internet")
           }
                
            } else {
             
            DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {  
                self.InternetConnectionAlert("Warnning", "We are not connected to internet")
            }
            }

            print(path.isExpensive)
        }
        
        private func InternetConnectionAlert(_ title : String, _ Body : String) {
            
            let alert = UIAlertController(title: title, message: Body, preferredStyle: .alert)
            
            alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
                  switch action.style{
                  case .default:
                    print("cancel")
                
                  case .cancel:
                        print("cancel")

                  case .destructive:
                        print("destructive")
            }}))
            
            UIApplication.shared.keyWindow?.rootViewController?.present(alert,animated: true,completion: nil)
            
        }

if you want to stop network monitoring

monitor.cancel()

Present alert on window in swift 5

how to present alert in window in swift

func showAlert(_ title: String, _ message: String) {
        let alert = UIAlertController(
            title: title, 
            message: message, 
            preferredStyle: UIAlertControllerStyle.alert
        )

        alert.addAction(UIAlertAction(
            title: "OK", 
            style: UIAlertActionStyle.default, 
            handler: nil
        ))

       alert.addAction(UIAlertAction(
            title: "Cancel", 
            style: UIAlertActionStyle.default, 
            handler: nil
        ))

        UIApplication.shared.keyWindow?.rootViewController?.present(
            alert, 
            animated: true, 
            completion: nil
        )
    }

Get tableview height using observer in swift

first of first set observer in viewdidLoad using blow code

tableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)

Then add observer value function for getting tableview height whenever content is updated

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if (object as! UITableView) == self.tableView {
      if(keyPath == "contentSize"){
        if let newvalue = change?[.newKey]
        {
          let newsize = newvalue as! CGSize
          print(newsize.height)
          //yourtableview.height = newsize.height 
        }
      }
    }
  }

In last remove observer from viewwillDisapper

if tableView.observationInfo != nil{
      tableView.removeObserver(self, forKeyPath: "contentSize")
    }

Developed by : Krina Kathiriya