2011-07-27 19 views
0

在我的應用程序中,我使用的是webservices。我已經使用Reachability類實現了網絡可達性。但是,只有當我開始我的申請時纔會檢查。如何實現信號丟失或站點down的代碼?

請注意,在下載數據時如果信號丟失或站點發生故障,我想顯示警報窗口。

這可以通過使用Reachability類來完成嗎?如果是,而不是如何實現這一點?如果沒有比其他方式實施更多?

在此先感謝。

回答

2

是的,你可以用NSNotificationCenter

- (void) addReachability 
{ 
    //Use the Reachability class to determine if the internet can be reached. 
     [[Reachability sharedReachability] setHostName:kHostName]; 

    //Set Reachability class to notifiy app when the network status changes. 
    [[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES]; 

    //Set a method to be called when a notification is sent. 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) 
               name:@"kNetworkReachabilityChangedNotification" object:nil]; 

} 

- (void)reachabilityChanged:(NSNotification *)note 
{ 
    [AppDelegate updateStatus]; 
} 
+0

我是否需要添加方法的幫助做到這一點? – Harsh

+0

請參閱Reachability.m小心你會得到你的答案 – Maulik

+0

其實我指的是它。但我沒有得到什麼 - (BOOL)startNotifier是什麼... – Harsh

0

是的,它可以做到。一旦你註冊獲得狀態,任何改變都會觸發回調。相應地處理它。 (BOOL)startNotifier -