1

しばらく検索して色々試してみましたが、このエラーを解消することはできません。プルダウン方式で更新されたテーブルがあります。また、AppleのReachabilityを使用してインターネット接続を決定します。インターネットを使用してアプリケーションを実行し、アプリの実行中にインターネットをオフにすると、UIAlertViewを表示しようとしてアプリがクラッシュします。ただし、インターネットなしでアプリを起動し、アプリの実行中にインターネットをオンにしてからオフに戻すと、アプリがクラッシュせず、すべてが正常に機能します。どんな助けでも大歓迎です。

[messageshow]行でエラーが発生します。

編集されたコード:

- (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];
}
4

2 に答える 2

0

2 つの問題があります。

  1. インターネット接続があるかどうかを判断するために到達可能性を使用しないでください。到達可能性は、オフライン接続がいつオンラインに戻るかを判断する場合にのみ役立ちます。本当。ネットワーク コールを行うだけで、ラジオの電源を入れて接続するのに十分な場合があるため、インターネット接続があるかどうかを事前に確認しようとすると、ラジオはオフのままになります。

  2. 2 つ目の問題は、メイン スレッドで同期ネットワーク呼び出しを行うことができないことです。これには、到達可能性と [NSData dataWithContentsOfURL:xxx] が含まれます。Grand Central Dispatch、NSOperationQueues、NSURLConnections、または NSThreads を使用して、セカンダリ スレッドでそれらを作成します。そうしないと、ネットワークが良好な状態になく、システムのウォッチドッグ タイマーがアプリを強制終了すると、アプリケーションがロックされる可能性があります。

于 2012-10-03T03:13:38.660 に答える
0

URLからデータをダウンロードする方法として、 sendAsynchronousRequest:queue:completionHandler: を使用して、次の方法をテストしました。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];
        }
    }];
于 2012-10-03T05:32:01.153 に答える