2016-04-28 19 views
1

我有一個我正在處理的應用程序與Parse鏈接,所以我使用的是PFQueryTableViewController在Swift中檢查與PFQueryTableViewController的互聯網連接

我只想知道如何在運行應用程序時檢查互聯網連接。

目前,如果我關閉了我的互聯網連接,然後運行應用程序,我得到表的加載微調,但顯然,沒有加載。最終,微調停下來,我只剩下一張空白的桌子。

我讀過關於使用Reachability,但不確定如何將它放入我目前的應用程序。

本質上,我想要的是,每次用戶啓動應用程序,它都會檢查互聯網連接,如果那樣的話就很棒!如果沒有,它會顯示一條提示,表示無連接。

有人可以幫忙嗎?我已經爲queryForTable()函數添加了我的代碼,我認爲這個檢查應該發生。如果您需要查看我的其他代碼,請告訴我。由於

override func queryForTable() -> PFQuery { 

    let query = PFQuery(className: "Reviews") 

    if indexArray == 0 { 

     query.orderByDescending("createdAt") 

    } else if indexArray == 1 { 

     query.orderByAscending("FilmName") 

    } else if indexArray == 2 { 

     query.orderByDescending("OurRating") 

    } else if indexArray == 3 { 

     query.orderByAscending("OurRating") 

    } else if indexArray == 4 { 

     query.orderByDescending("WantToSeeThisCount") 

    } else if indexArray == 5 { 

     query.orderByAscending("DirectedBy") 

    } else if indexArray == 6 { 

     query.orderByDescending("UpVoteCount") 

    } else if indexArray == 7 { 

     query.orderByDescending("DownVoteCount") 

    } 

    query.whereKey("ToDisplayInApp", equalTo:true) 

    // Add a where clause if there is a search criteria 
    if filmSearchBar.text != "" { 

     query.whereKey("FilmName", containsString: filmSearchBar.text!) 

    } 

    return query 

} 
+0

這可以幫助你[可達](HTTP:/ /stackoverflow.com/a/34128493/3409505) –

+0

我建議測試互聯網是否可以訪問,每當你想進行網絡請求時,不僅僅是當用戶打開應用程序時,可達性是最簡單的解決方案,它不能簡單的.. .. –

回答

0
var reachability: Reachability! 

func checkNetworkReachability() { 
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(functionToHandleWhenConnectionChanges()), name:kReachabilityChangedNotification, object: nil) 

    if self.reachability == nil { 
     self.reachability = Reachability.reachabilityForInternetConnection() 
     self.reachability.startNotifier() 
    } 
} 

func functionToHandleWhenConnectionChanges() { 
    // self.reachability.isReachable() returns a boolean. If it's yes, then you are connected to internet, otherwise not connected. 
} 

之前做這一切,從https://github.com/tonymillion/Reachability

希望添加Reachability.h和Reachability.m文件這有助於:)