0

次のような NSURL があります。

NSURL *url = [NSURL URLWithString:@"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];

しかし、インターネット接続や電話信号がない場合、アプリがクラッシュします。使用前に最初に接続をテストする方法はありますか?

試してキャッチする必要がありますか?について読んNSURLConnectionでいますが、それがどのように機能するかは不明です。何か案は?

私が得ているエラーはThread 6:signal SIGABRT、これが何を意味するのか分かりません。以下のすべての例を試しましたが、どれもうまくいきませんでした。

私はこれを追加しました:

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    // release the connection, and the data object
    // receivedData is declared as a method instance elsewhere

    [connection release];

    // inform the user
    UIAlertView *didFailWithErrorMessage = [[UIAlertView alloc] initWithTitle: @"NSURLConnection " message: @"didFailWithError"  delegate: self cancelButtonTitle: @"Ok" otherButtonTitles: nil];
    [didFailWithErrorMessage show];
    [didFailWithErrorMessage release];

}

インターネットから Mac のプラグを抜き、シミュレーターでアプリを実行すると、これが表示されますThread 6:signal SIGABRT

以下のコードを使用しました

Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
    NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
    if (networkStatus == NotReachable) {
        dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Announcement" message: @"No Connection" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release];
        });
    } else {
        dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Announcement" message: @"Connection" delegate: nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release];
        });

入力に接続している場合は機能しますが、インターネットをオフにするとこのエラーが発生し、else アラートが表示されません

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
4

3 に答える 3

0

Apple が提供するサンプル コードを使用して、到達可能性をテストできます。

http://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

必要なホスト名で到達可能性を初期化すると、次のことができます。

  • 通知機能を開始/停止すると、このホストへの到達可能性が変更されたかどうかを示す通知を受け取ります

  • または単に現在の到達可能性ステータスを取得する

Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];
if (internetStatus == NotReachable) {
    // You have internet
} else {
    // You do not have internet
}
于 2013-07-15T01:13:52.323 に答える
0

私がやりたいことの 1 つは、別の NSURL との接続を確認し、接続の BOOL を変更するかどうかです。私はこのような方法を作りました..

- (void)checkConnection
{
NSURL *checkURL = [NSURL URLWithString:@"http://www.apple.com"];
NSData *data = [NSData dataWithContentsOfURL:checkURL];

if (data)
{
    _connected = YES;
}
else
{
    _connected = NO;
}
}

これで、接続されているかどうかを知らせる BOOL _connected ができました。次に、最初に接続されていることを確認する必要がある方法で、次のようなことを行います..

- (void)viewWillAppear:(BOOL)animated
{
[self checkConnection];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

if (_connected == YES)
{
    [self fetchTweets];
    [self reFresh];
    [self.tableView reloadData];
}else
{
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"No Internet"
                                                      message:@"You must have an internet connection."
                                                     delegate:self
                                            cancelButtonTitle:@"OK"
                                            otherButtonTitles:nil];
    [message show];
    [self.refreshControl endRefreshing];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    return;
}
}

それが、私が書いたいくつかのアプリに対して行ったことです。迅速で、大量のコードやインポートは必要ありません。または、NSURLConnection と NSURLRequest を調べることもできます。組み合わせて、データがない場合に警告などを表示できるメソッドがあります。それはロブが話していたことです。ちょっとした考え!

于 2013-07-15T01:32:47.933 に答える
0

Apple の Reachability コードの Tony Million のフォークは、この問題に対する優れたブロックベースのソリューションです: https://github.com/tonymillion/Reachability

// allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];

// set the blocks 
reach.reachableBlock = ^(Reachability*reach)
{
    NSLog(@"REACHABLE!");
};

reach.unreachableBlock = ^(Reachability*reach)
{
    NSLog(@"UNREACHABLE!");
};

// start the notifier which will cause the reachability object to retain itself!
[reach startNotifier];
于 2013-07-15T01:35:26.240 に答える