我已在this answer之後實施了可達性類別。 我有兩個不同的問題,我希望對你有所幫助......檢查iPhone上的互聯網連接並監視更改
我已經把通知我viewDidLoad中:
(void)viewDidLoad
{
[super viewDidLoad];
// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [Reachability reachabilityWithHostName: @"www.apple.com"];
[hostReachable startNotifier];
}
,我查一下我的viewDidAppear互聯網連接:
(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//check connection when access to the view
if (internetActive)
{
[self loadData];
}
else
{
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error loading content" message:@"Internet connection not available" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
}
第一個問題:如何檢查互聯網連接首次
我第一次ACC如果網絡連接正常,則顯示警報。我認爲,這是因爲通知程序尚未收到關於連接的通知。如何管理這個?
第二個問題:如果連接狀態改變(即從不存在至今),通知並沒有徵詢關於此
如果我開始沒有互聯網連接的應用程序,顯示正確的警報。但是如果互聯網再次可用,每次訪問視圖時都會繼續顯示警報,因爲互聯網可用性的更改不會被攔截。我認爲這是由於在同一線程上執行通知程序。如何實現此連接檢查以攔截連接狀態更改?
在此先感謝和新年快樂, yassa
之前,您應該看一看[可達沒有吸(https://github.com/ nickpaulson/NPReachability)。 – samvermette 2011-12-31 19:14:55