试图在我的应用程序中实现网络错误警报? [英] Trying to implement network error alerts into my app?

查看:83
本文介绍了试图在我的应用程序中实现网络错误警报?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我想让这个应用程序显示网络错误警报代码。我添加了SystemConfiguration.framework框架和Apple的Reachability示例代码。

Ok so I am trying to get this app to show network error alert codes. I have added the SystemConfiguration.framework framework and Apple's "Reachability" sample code.

这是viewcontroller.h文件:

Here is the viewcontroller.h file:

#import <UIKit/UIKit.h>

@class Reachability;

@interface Test_Internet_ConnectionViewController : UIViewController {

    Reachability* internetReachable;
    Reachability* hostReachable;
}

@property BOOL internetActive;
@property BOOL hostActive;

- (void) checkNetworkStatus:(NSNotification *)notice;

@end

这是viewcontroller.m文件:

Here is the viewcontroller.m file:

#import "Test_Internet_ConnectionViewController.h"
#import "Reachability.h";

@implementation Test_Internet_ConnectionViewController

@synthesize internetActive;
@synthesize hostActive;

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

        // check for internet connection
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

        internetReachable = [[Reachability reachabilityForInternetConnection] retain];
        [internetReachable startNotifier];

        // check if a pathway to a random host exists
        hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
        [hostReachable startNotifier];

        // now patiently wait for the notification
    }

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void) checkNetworkStatus:(NSNotification *)notice
{
    // called after network status changes

    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)

    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");
            self.internetActive = NO;

            UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Internet Unavailable" message:@"This page cannot load, please check your internet connection and try again." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
            [errorView show];
            [errorView release];

            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");
            self.internetActive = YES;

            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");
            self.internetActive = YES;

            break;

        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)

    {
        case NotReachable:
        {
            NSLog(@"A gateway to the host server is down.");
            self.hostActive = NO;

            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"A gateway to the host server is working via WIFI.");
            self.hostActive = YES;

            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"A gateway to the host server is working via WWAN.");
            self.hostActive = YES;

            break;

        }
    }
}

- (void)dealloc {
    [super dealloc];

        [[NSNotificationCenter defaultCenter] removeObserver:self];

    }

@end

如果有打开应用程序时没有互联网错误按预期工作,但如果应用程序在最初打开网络连接后失去连接,则错误显示3次,此时我显然只希望显示一次。

If there is no internet upon opening the app the error works as intended, but if the app loses connection after the it has initially opened with an internet connection the error displays 3 times, when I obviously only want it to display once.

有人可以帮帮我吗?
提前谢谢。

Can anyone help me, please? Thanks in advance.

推荐答案

错误视图委托应该是自我而不是零,但我不是确定是否能解决问题。如果没有,问题可能是 NSNotificationCenter 向它的观察者发送了太多通知(在这种情况下 checkNetworkStatus 。最简单(但可能不那么优雅)的解决方案是保持一个通知计数器变量,当显示错误视图时,该变量会增加,并且只有在计数器为零时才显示错误View。更好的解决方案是检查传入的通知,并以不同的方式回应不同的通知(或根本不回应)。

The error View delegate should probably be self and not nil, but I'm not sure if that will solve the problem. If it doesn't, the issue is probably that NSNotificationCenter is sending too many notifications to it's observer (in this case checkNetworkStatus. The simplest (but probably less elegant) solution, is to keep a notice counter variable that is incremented when the error View is displayed, and only display the error View if the counter is zero. The better solution is to check the incoming notices, and respond to different notices differently (or not at all).

这篇关于试图在我的应用程序中实现网络错误警报?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆