如何解决 iPhone 中的互联网检查问题?

发布于 2024-11-19 03:05:47 字数 1346 浏览 2 评论 0原文

我正在开发一个应用程序,我必须检查互联网连接。我正在使用两个文件 Reachability.h 和 Reachability.m。现在,当互联网无法正常工作时,我必须显示警报。

但是当我的观察者检查互联网时,它多次显示警报视图。有时它会显示警报 9-10 次。 我的代码在这里:

在 .h 文件

Reachability* internetReachable;
    Reachability* hostReachable;
    NetworkStatus internetStatus;

中 .m 文件中,

        internetReachable = [[Reachability reachabilityForInternetConnection] retain];
        [internetReachable startNotifier];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
        hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
        [hostReachable startNotifier];



- (void) checkNetworkStatus:(NSNotification *)notice    
     {
        internetStatus = [internetReachable currentReachabilityStatus];

        if (internetStatus != NotReachable){
            NSLog(@"internet is on");

        }
        else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Server not connected or down!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];
            internetCount++;
        }           
}

此代码位于委托中,类似地,我在其他类中使用此代码,我必须检查互联网

请帮助

I am working on an application and i have to check internet connectivity. I am using two files Reachability.h and Reachability.m. Now i have to show an alert when internet is not working.

But when my observer check the internet it shows alert view more then one time. and some times it shows the alert more 9-10 times.
my code is here:

in .h file

Reachability* internetReachable;
    Reachability* hostReachable;
    NetworkStatus internetStatus;

in .m file

        internetReachable = [[Reachability reachabilityForInternetConnection] retain];
        [internetReachable startNotifier];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
        hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
        [hostReachable startNotifier];



- (void) checkNetworkStatus:(NSNotification *)notice    
     {
        internetStatus = [internetReachable currentReachabilityStatus];

        if (internetStatus != NotReachable){
            NSLog(@"internet is on");

        }
        else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!" message:@"Server not connected or down!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];
            internetCount++;
        }           
}

this code is in delegate and similarly i am using this code in other classes where ever i have to check internet

plz help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

权谋诡计 2024-11-26 03:05:47

我不使用 Reachability 因为我遇到了像你这样的问题,所以我创建了自己的实现。只需导入下面的头文件,然后调用 [self networkIsReachable] 来检查互联网连接。

    #import <netinet/in.h>
    - (BOOL)networkIsReachable 
    {
        struct sockaddr_in zeroAddress;
        bzero(&zeroAddress, sizeof(zeroAddress));
        zeroAddress.sin_len = sizeof(zeroAddress);
        zeroAddress.sin_family = AF_INET;

        SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
        SCNetworkReachabilityFlags flags;
        BOOL gotFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
        CFRelease(defaultRouteReachability);
        if (!gotFlags) {
            return NO;
        }
        BOOL isReachable = flags & kSCNetworkReachabilityFlagsReachable;

        BOOL noConnectionRequired = !(flags & kSCNetworkReachabilityFlagsConnectionRequired);
        if ((flags & kSCNetworkReachabilityFlagsIsWWAN)) {
            noConnectionRequired = YES;
        }

        return (isReachable && noConnectionRequired) ? YES : NO;
    }

I don't use Reachability because I've had problems like you, so I created my own implementation. Just import the header file below and then call [self networkIsReachable] to check for an internet connection.

    #import <netinet/in.h>
    - (BOOL)networkIsReachable 
    {
        struct sockaddr_in zeroAddress;
        bzero(&zeroAddress, sizeof(zeroAddress));
        zeroAddress.sin_len = sizeof(zeroAddress);
        zeroAddress.sin_family = AF_INET;

        SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);
        SCNetworkReachabilityFlags flags;
        BOOL gotFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);
        CFRelease(defaultRouteReachability);
        if (!gotFlags) {
            return NO;
        }
        BOOL isReachable = flags & kSCNetworkReachabilityFlagsReachable;

        BOOL noConnectionRequired = !(flags & kSCNetworkReachabilityFlagsConnectionRequired);
        if ((flags & kSCNetworkReachabilityFlagsIsWWAN)) {
            noConnectionRequired = YES;
        }

        return (isReachable && noConnectionRequired) ? YES : NO;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文