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

我喜欢做的一件事是检查与不同 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

Tony Million 对 Apple 的 Reachability 代码的 fork 是一个很棒的、基于块的解决方案: 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 回答