iOS UIAlertView提供EXC_BAD_ACCESS

时间:2012-10-03 02:52:46

标签: ios uialertview exc-bad-access reachability

我已经搜索了一段时间并尝试了很多东西,但我不能让这个错误消失。我有一个通过下拉方法刷新的表。我还使用Apple的Reachability来确定互联网连接。当我使用互联网运行我的应用程序,然后在应用程序运行时,关闭互联网,我的应用程序崩溃尝试显示UIAlertView。虽然,当我启动我的应用程序没有互联网,然后在应用程序运行时打开互联网并关闭它,应用程序不会崩溃,一切正常。任何帮助将不胜感激。

[message show]行发生错误。

编辑代码:

- (void)loadCallList {

NSURL *theURL = [NSURL URLWithString:@"http://www.wccca.com/PITS/"];

NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if (theConnection) {

    NSLog(@"Reachable");

    self.headerHeight = 45.0;

    NSData *data = [[NSData alloc] initWithContentsOfURL:theURL];
    xpathParser = [[TFHpple alloc] initWithHTMLData:data];
    NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']//@value"];

        if (elements.count >= 1) {

            TFHppleElement *element = [elements objectAtIndex:0];
            TFHppleElement *child = [element.children objectAtIndex:0];
            NSString *idValue = [child content];

            NSString *idwithxml = [idValue stringByAppendingFormat:@".xml"];
            NSString *url = @"http://www.wccca.com/PITS/xml/fire_data_";
            NSString *finalurl = [url stringByAppendingString:idwithxml];

            xmlParser = [[XMLParser alloc] loadXMLByURL:finalurl];

            [callsTableView reloadData];
        }
    }

else {

    NSLog(@"Not Reachable");

    self.headerHeight = 0.0;

    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"No Internet Connection"
                                                      message:@"Please check your internet connection and pull down to refresh."
                                                     delegate:self
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
    [message show];
    [message release];
}

[pull finishedLoading];
}

2 个答案:

答案 0 :(得分:0)

你有两个问题:

  1. 您不应使用Reachability来确定是否存在互联网连接。可达性仅用于确定脱机连接何时重新联机。真。进行联网呼叫有时足以启动无线电并连接,因此如果您尝试预先检查是否存在互联网连接,则无线电将保持关闭状态。

  2. 第二个问题是不能在主线程上进行同步网络调用。这包括Reachability和[NSData dataWithContentsOfURL:xxx]。使用Grand Central Dispatch,NSOperationQueues,NSURLConnections或NSThreads在辅助线程上创建它们。否则,如果网络状况不佳,您的应用程序可能会锁定,系统监视程序计时器将终止您的应用程序。

答案 1 :(得分:0)

我使用sendAsynchronousRequest测试了以下方法:queue:completionHandler:作为从URL下载数据的方法。它似乎工作正常,我可以通过搞乱URL或超时间隔.1秒来触发else子句。

    NSURL *theURL = [NSURL URLWithString:@"http://www.wccca.com/PITS/"];

    NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5];
    [NSURLConnection sendAsynchronousRequest:theRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if (error == nil) {
            //do whatever with the data.  I converted it to a string for testing purposes.
            NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@",text);
        }else{
            NSLog(@"%@",error.localizedDescription);
            UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"Please check your internet connection and pull down to refresh." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [message show];
        }
    }];