我已實施Reachability Api 2.2。當網絡從關閉狀態變爲開啓狀態時,它不會觸發。可達性類別在iOS中不起作用
此外,我可以在應用程序委託中實現它嗎?如果是這樣,我應該在哪裏刪除觀察者?
這裏是我的代碼(這不叫開除模型的viewController)
- (void)viewDidLoad
{
// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
internetReachable = [[Reachability reachabilityForInternetConnection] retain] ;
[internetReachable startNotifier];
// check if a pathway to a random host exists
hostReachable = [[Reachability reachabilityWithHostName: @"www.google.com"] retain];
[hostReachable startNotifier];
}
- (void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus hoststatus=[hostReachable currentReachabilityStatus];
NetworkStatus internetStatus=[internetReachable currentReachabilityStatus];
for (NSString *msg in messageArray) {
[stringg appendString:[NSString stringWithFormat:@"%@",msg]];
}
if (hoststatus==NotReachable||internetStatus==NotReachable) {
[self.navigationController presentModalViewController:inter animated:YES];
}
else{
[self dismissModalViewControllerAnimated:YES];
}
[inter release];
}
- (void)viewDidUnload
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super viewDidUnload];
}
你正在泄漏'internetReachable';有一個'保留'沒有配對'釋放'/'autorelease' –