1

在我的 iPhone 应用程序中,我需要检测互联网连接的可用性。

所以我引用了 Apple 的“Reachability”项目中的一些文件。

链接如下:

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

我创建了一个新项目并在 viewWillAppear 中实现了以下代码,但应用程序崩溃了。

我包含了来自 Apple 演示项目的 Reachability.h、Reachability.m。

我还包括了 SystemConfiguration 框架。

互联网工作时,应用程序工作正常。但是当互联网不工作时应用程序崩溃。

即使我检查了控制台,但控制台中没有显示任何通知或错误。

   Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];

NetworkStatus internetStatus = [r currentReachabilityStatus];
if ((internetStatus == ReachableViaWiFi) || (internetStatus == ReachableViaWWAN))
{
    UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"Internet Connection" message:@"Available" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [myAlert show];
    [myAlert release];
    }
else
{
    UIAlertView *myAlert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"This app require an internet connection via WiFi or cellular network to work." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [myAlert show];
    [myAlert release];
    }

崩溃的原因可能是什么?

我该怎么办?

谢谢!!

4

3 回答 3

4

您过度释放警报。首先,你做autorelease然后另外release,这太多了。只需删除这两个[myAlert release];,它应该可以工作。

于 2010-11-26T12:21:01.857 回答
1

在这里,您的代码可能由于超时而无法在模拟器上运行。但是尝试在设备上运行它。还可以尝试按照@greg 正确的说法调试代码。对于那个信用应该去@greg。希望这可以帮助。让我知道它是否有效。

于 2010-11-28T04:27:10.603 回答
0

您的问题出在代码的其他地方,因为您提供的代码以及将 Reachability.[mh] 复制到新的 Xcode 项目中似乎可以正常工作而不会崩溃。当您的应用程序崩溃时,它必须在控制台中记录某种信息。如果真的什么都没有,请在“启动”方法中设置一个断点(viewWillLoad, viewDidLoad,AppDelegate东西并逐行执行,直到发现问题。

于 2010-11-28T00:51:52.373 回答