2012-01-29 61 views
2

我正在使用ASIHTTPRequest。我需要知道如何使用它來完成以下事件;檢查是否可用WIFI使用ASIHTTPRequest

以下是我應該檢查互聯網可用性的方式,但在哪些情況下我應該添加它以解決以下問題;

self.remoteHostStatus   = [[Reachability sharedReachability] remoteHostStatus]; 
self.internetConnectionStatus = [[Reachability sharedReachability] internetConnectionStatus]; 
self.localWiFiConnectionStatus = [[Reachability sharedReachability] localWiFiConnectionStatus]; 

1)應用,如Foursquare的彈出警報只要互聯網將丟失?這是如何完成的,在什麼情況下我應該編碼? (用戶可能會執行任務,突然的WiFi熄滅,那麼我應該彈出一個警告,說沒有可用的WIFI)一旦

2)互聯網/無線網絡連接回我需要刷新和更新數據。這是如何完成的? (它應該不斷收聽,一旦互聯網回來,它應該更新該視圖)

回答

3

有一個通知發佈稱爲kReachabilityChangedNotification

您可以通過使用可訪問性的

- (BOOL)startNotifier; 
- (void)stopNotifier; 

先註冊該通知執行該通知的產生 - 例如在UIViewControllerviewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(reachabilityChangedNotificationSent:) 
              name:kReachabilityChangedNotification 
              object:nil]; 

添加下面的方法 - 例如到您的UIViewController實施。

- (void)reachabilityChangedNotificationSent:(NSNotification *)notification 
{ 
    NSLog(@"reachability changed: %@", notification.userInfo); 
} 

現在只需調用startNotifier - 例如在viewDidLoad再次每當任何改變發生,你應告知。

[[Reachability sharedReachability] startNotifier]; 

不要忘記從通知中刪除自己,並在完成後調用stopNotifier。堅持我的例子意味着你應該添加這個到你的viewDidUnload實現。

[[Reachability sharedReachability] stopNotifier]; 
[[NSNotificationCenter defaultCenter] removeObserver:self 
               name:kReachabilityChangedNotification 
               object:nil]; 
+0

你能告訴我如何調用使用''startNotifier使用「stopNotifier」中刪除我自己(我是初學者)我應該在哪裏添加'[可達sharedReachability] startNotifier]'? – Illep 2012-01-29 15:20:20

+0

看到編輯答案,希望有所幫助。 – Till 2012-01-29 15:27:00

+0

好的,所以我必須在'viewDidLoad'方法內寫入除'(reachabilityChangedNotificationSent')之外的所有代碼? – Illep 2012-01-29 15:34:34