0
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(myReachabilityDidChangedMethod) 
              name:kReachabilityChangedNotification 
              object:nil]; 
Reachability *reachability; 
reachability = [Reachability reachabilityForInternetConnection]; 
[reachability startNotifier]; 

我上面的代碼塊在我的AppDelegate創建可達性的觀察者,其目的是通過應用程序觸發myReachabilityDidChangedMethod。觀察員在AppDelegate的可達性不能觸發

但是,我無法觸發位於AppDelegate中的myReachabilityDidChangedMethod,當我打開或關閉我的wifi時,我在模擬器和ipad上都測試了它,但兩者都沒有任何響應。

回答

-1

要觸發你的方法,你需要發佈一個通知:

[NSNotificationCenter defaultCenter] postNotificationName:@ 「kReachabilityChangedNotification」 對象:無]。

順便說一句,如果我沒有記錯的話,在您註冊的通知,kReachabilityChangedNotification應寫爲字符串@「kReachabilityChangedNotification」

+0

不應該的可達性類本身自動發佈通知? – Shing 2012-07-11 19:30:33

+0

@Shing,不,它不會。用戶必須在需要時明確向通知中添加項目。還要確保在應用程序退出時刪除通知。使用此命令刪除所有通知:'[[NSNotificationCenter defaultCenter] removeObserver:self];'或者刪除特定通知:'[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];' – Steven 2012-08-20 13:59:35