0

I'm trying to check at the launch of the app whether the internet is connected or not. When I switch of my Wifi off and check the work around the app the app is still trying to connect to the internet for 10 seconds. If user is not connected to the internet then I should display an alert that please connect to the internet.

I'm not using localDataStore. In my opinion I have to add an internet check statement in queryForTable.

func baseQuery() -> PFQuery {
    let query = PFQuery(className: "homeClass") // Replace with the class name for the Parse data you're interested in.
     // Just like with a regular PFQuery you can filter and sort.
    query.orderByAscending("SNo")
    return query
} 


override func queryForTable() -> PFQuery {
    if Reachability.isConnectedToNetwork() == true {
        print("Internet connection OK")
        tableView.reloadData()
        self.baseQuery()
    } else {
        print("Internet connection FAILED")
        let alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
        alert.show()
        let destinationVC:MainViewController = self.storyboard?.instantiateViewControllerWithIdentifier("NewView") as! MainViewController
        navigationController?.pushViewController(destinationVC , animated: true)

    }

}

Update

func baseQuery() -> PFQuery {

    let query = PFQuery(className: "homeClass")

    if Reachability.isConnectedToNetwork() != true {
       print("Internet connection FAILED")

            } else {
            print("Internet connection OK")
            query.orderByAscending("SNo")
            return query
        }

        return PFQuery()
    }

  override func queryForTable() -> PFQuery {

    return self.baseQuery()
}

I'm trying this code and still it is trying to fetch data from the internet.

Sabhay Sardana
  • 876
  • 1
  • 10
  • 27

3 Answers3

0

I just found this on SO this procedure to check the interface IP address, you need to add a bridging header to make this work.

Although not perhaps "the answer"; you should be able to modify this code to check the status of all the network connections.

Note you need to include this thru a objective-c bridging header.

#include <ifaddrs.h>

You should be able get it together from here!

func getIFAddresses() -> [String] {
var addresses = [String]()

// Get list of all interfaces on the local machine:
var ifaddr : UnsafeMutablePointer<ifaddrs> = nil
if getifaddrs(&ifaddr) == 0 {

// For each interface ...
for (var ptr = ifaddr; ptr != nil; ptr = ptr.memory.ifa_next) {
    let flags = Int32(ptr.memory.ifa_flags)
    var addr = ptr.memory.ifa_addr.memory

    // Check for running IPv4, IPv6 interfaces. Skip the loopback interface.
    if (flags & (IFF_UP|IFF_RUNNING|IFF_LOOPBACK)) == (IFF_UP|IFF_RUNNING) {
        if addr.sa_family == UInt8(AF_INET) || addr.sa_family == UInt8(AF_INET6) {

            // Convert interface address to a human readable string:
            var hostname = [CChar](count: Int(NI_MAXHOST), repeatedValue: 0)
            if (getnameinfo(&addr, socklen_t(addr.sa_len), &hostname, socklen_t(hostname.count),
                nil, socklen_t(0), NI_NUMERICHOST) == 0) {
                    if let address = String.fromCString(hostname) {
                        addresses.append(address)
                    }
            }
        }
    }
}
freeifaddrs(ifaddr)
}

 return addresses
}
user3069232
  • 8,587
  • 7
  • 46
  • 87
  • for this do I have to create a new file ? as when I run my code , the first function that is executed is the `func queryForTable() -> PFQuery` – Sabhay Sardana Mar 12 '16 at 08:24
  • If your function relies on a network connection than you need to run something like this before you run a query. – user3069232 Mar 12 '16 at 10:50
0

I prefer and most of the people prefer Reachability framework. try this Check for internet connection with Swift

Community
  • 1
  • 1
Mazel Tov
  • 2,064
  • 14
  • 26
0

I use Reachability in my App and it's great! You can check if KNOTREACHABLE is true or false each time a user taps on an button. Reachability uses NSNotification to constantly check if the user is connected to WiFi, cellular, or disconnected. You can even have a label or notification pop up is the connection is lost. Reachability Link

There are a few good YouTube videos out there...here is one:

Reachability Swift YouTube Video

Dan Levy
  • 3,931
  • 4
  • 28
  • 48