那里有任何可达性专家吗?

时间:2011-12-02 21:36:30

标签: iphone crash reachability internet-connection

我的应用程序运行良好,直到我将Reachability .h和.m文件合并到它来检查互联网连接。虽然在大多数情况下一切都运行得很好(所有网页都正确加载,当我故意关闭我的机场进行测试时,Reachability优雅地抓住它) - 我现在在奇怪的时候遇到莫名其妙的崩溃,带着可怕的“ Exc-Bad-Access“错误...... 我跑了仪器,找到了僵尸 - 看到屏幕抓取: Note the RefCt column - the count reached 20!

查看“RefCt”列,您可以看到它达到15 - 我看到它高于20! 在“负责的来电者”栏目(最右边),我看到了各种各样的方法,我不认识 - 以及谁打电话我当然没有发起 - 我假设他们在运行期间被系统内部调用-time?

无论哪种方式,我都遵循Apple的Reachability代码&非常仔细的说明,以及这个排名很高的stackoverflow线程的提示: How to check for an active Internet connection on iOS or OSX?

但我仍然遇到这些莫名其妙的崩溃。

有人可以提供任何建议吗?

以下是代码:

#import "Reachability.h"


-(void) viewWillAppear:(BOOL)animated
{
   // 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.apple.com"] retain];
[hostReachable startNotifier];

// now patiently wait for the notification
}



-(void) viewDidLoad {

    url = [NSURL URLWithString: @"http://www.google.com"];
    NSURLRequest *req = [NSURLRequest requestWithURL: url];
    [webPageView loadRequest:req];
    [super viewDidLoad];
}



-(void) checkNetworkStatus:(NSNotification *)notice
{
        // called after network status changes

    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];

    switch (internetStatus) 
    {
        case NotReachable:
        {
            [self displayMessageWithTitle:@"ERROR"
                               andMessage:@"Unable To Connect to Internet"
                              andOKButton:@"OK"];
            [self dismissModalViewControllerAnimated:YES];
            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");

            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");

            break;

        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)
    {
        case NotReachable:
        {
            // NSLog(@"A gateway to the host server is down.");

            [self displayMessageWithTitle:@"ERROR"
                               andMessage:@"Host Not Reachable"
                              andOKButton:@"OK"];
            [self dismissModalViewControllerAnimated:YES];
            break;

        }

        case ReachableViaWiFi:
        {
            NSLog(@"A gateway to the host server is working via WIFI.");

            break;

        }

        case ReachableViaWWAN:
        {
            NSLog(@"A gateway to the host server is working via WWAN.");

            break;

        }
    }
}



- (void)webViewDidStartLoad:(UIWebView *)webView {
    [activityIndicator startAnimating];
}


- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [activityIndicator stopAnimating];
}


// Since this ViewController is presented Modally, this method removes it 
// via a button click:
-(IBAction) dismissWebTixView:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}




-(void) viewWillDisappear:(BOOL)animated {
    NSLog(@"In webForTix's 'viewWillDisappear' method....");
    [[NSNotificationCenter defaultCenter] removeObserver:self
name:kReachabilityChangedNotification
                                              object:nil];
}


// Was told it might be good to put "removeObserver" here and not in viewWillDisappear
// well neither worked - the App still crashes....
- (void)dealloc
{
    //NSLog(@"In webForTix's dealloc method....");
//    [[NSNotificationCenter defaultCenter] removeObserver:self
//                                                        name:kReachabilityChangedNotification
//                                                  object:nil];
NSLog(@"In webForTix's dealloc method - removedObserver...");
[super dealloc];

}

1 个答案:

答案 0 :(得分:2)

您必须仔细平衡对addObserver和removeObserver的调用。

如果您在vieWillAppear中添加了通知的观察者,则需要在viewWillDisappear中删除它。

如果您添加观察者两次,则会因同一通知而被调用两次,并且必须两次调用removeObserver才能删除这两个通知。

我没有看到任何明显的内存管理问题。看起来您发布的所有代码都是创建自动发布的对象,这应该没问题。