【问题标题】:NSURL test connection before use [duplicate]使用前的NSURL测试连接[重复]
【发布时间】:2013-07-12 19:00:11
【问题描述】:

我有一个这样的 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];
        });

当我连接到 enter 时可以工作,但是当我关闭我的互联网时,我收到此错误并且不会出现 else 警报

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

【问题讨论】:

  • 是什么导致您的应用程序崩溃?不应该是这样,事件是你没有互联网。
  • 好吧,它并没有真正崩溃,我只是在寻找测试 NSURL 的连接,如果它失败则显示警报消息。
  • 您应该只创建连接并正确处理错误(例如检查返回值、处理 didFailWithError 等)。我认为写一个didFailWithError 会为你做的。
  • 我尝试了didFailWithError 并且弹出了这条消息Thread 6:signal SIGABRT 我需要在某处致电connection 吗?

标签: iphone


【解决方案1】:

您可以使用苹果提供的示例代码来测试可达性:

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
}

【讨论】:

  • 我需要导入和@implementation 吗?我在这条线 Reachability *reachability = [Reachability reachabilityForInternetConnection]; 和这条线 NetworkStatus internetStatus = [reachability currentReachabilityStatus]; 上遇到错误,这两个错误都说 undeclared identifier (Reachabilty or NetworkStatus)
  • 下载 Reachability 类并将其添加到您的项目中,我给了您链接...您必须导入 Reachability.h
  • 这不起作用,当我连接到互联网时,它会转到其他地方。如果我断开互联网连接并在模拟器上运行我的应用程序,我会收到此消息 Thread 6:signal SIGABRT
  • 这是有效的,请做一些研究...stackoverflow.com/questions/13735611/…
  • 我已经更新了我的问题,当我使用最后一段代码并关闭我的计算机上的互联网并运行模拟器时,我收到此错误erminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil' 并且没有出现警报。
【解决方案2】:

我喜欢做的一件事是检查与不同 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。结合起来,它们有一些方法可以让您在没有数据的情况下显示警告等。这就是罗布所说的。只是一个想法!

【讨论】:

  • @downvoter,为什么不在后面做一点解释,这样我们都可以学习?
  • 不要这样做。这很浪费,而且在你尝试过这个“检查”之后,互联网连接的状态随时都可以很容易地改变。
  • @Mike Abdullah,我明白你的意思是连接可以改变,但你的意思是浪费。浪费,是因为它可以改变还是因为它的编程不好?谢谢。
  • 访问一个不相关的站点会浪费带宽和电池电量,然后不会给你一个 100% 正确的答案。相反,只需访问真正的目标,并为处理故障做好适当的准备
  • @Mike Abdullah,感谢您提供的信息,我将确保在我的编程中摆脱它。 .
【解决方案3】:

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];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 2019-03-13
    • 1970-01-01
    • 2011-07-28
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    相关资源
    最近更新 更多