2016-03-12 150 views
0

我試圖檢查應用程序的啓動是否連接互聯網。 當我切換我的Wifi並檢查應用程序周圍的應用程序時,應用程序仍嘗試連接到互聯網10秒鐘。如果用戶沒有連接到互聯網,那麼我應該顯示一條警告,請連接到互聯網。我不是localDataStore。 在我看來,我必須在queryForTable添加互聯網檢查聲明。檢查互聯網連接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) 

    } 

} 

更新

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() 
} 

我想此代碼,它仍然試圖從互聯網上獲取數據。

回答

0

我剛剛發現了這個程序來檢查接口的IP地址,你需要添加一個橋接頭來使這個工作。

雖然不是「答案」;您應該能夠修改此代碼以檢查所有網絡連接的狀態。

請注意,您需要通過一個objective-c橋接頭來包含這一點。

#include <ifaddrs.h> 

你應該可以從這裏把它合起來!

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 
} 
+0

這個我必須創建一個新的文件?當我運行我的代碼時,執行的第一個函數是'func queryForTable() - > PFQuery' –

+0

如果你的函數依賴於網絡連接,那麼在運行查詢之前,你需要運行這樣的事情。 – user3069232

0

我在我的應用程序中使用可達性,這太棒了!每次用戶點擊按鈕時,您都可以檢查KNOTREACHABLE是否爲真。可達性使用NSNotification來不斷檢查用戶是否連接到WiFi,手機或斷開連接。如果連接丟失,您甚至可以彈出標籤或通知。 Reachability Link

有幾個不錯的YouTube視頻在那裏...這裏是一個:

Reachability Swift YouTube Video

+0

謝謝你的回答@丹,但我想解析queryForTable(),它不工作。 –