2009-12-14 52 views

回答

4

你可以只使用一個計時器並取消你的通知請求,如果沒有收到呢?

例如以蘋果的可達性爲例:

- (void) startNotifier 
{ 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onReachabilityChanged:) name:@"kNetworkReachabilityChangedNotification" object:nil]; 
    notified = NO; 
    [self performSelector:@selector(onRequestTimeout) withObject:nil afterDelay:5.0]; // 5 secs 
} 

- (void)onReachabilityChanged:(NSNotification *)note 
{ 
     // Do whatever on notification 
     notified = YES; 
} 

- (void) onRequestTimeout 
{ 
    if (!notified) 
    { 
     // Do whatever on request timeout 
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"kNetworkReachabilityChangedNotification" object:nil]; 
    } 
} 
相關問題