【问题标题】:Trying to implement network error alerts into my app?试图在我的应用程序中实现网络错误警报?
【发布时间】:2011-01-27 16:37:42
【问题描述】:

好的,我正在尝试让这个应用程序显示网络错误警报代码。我添加了 SystemConfiguration.framework 框架和 Apple 的“Reachability”示例代码。

这里是 viewcontroller.h 文件:

#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 文件:

#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 次,而我显然只希望它显示一次。

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

【问题讨论】:

    标签: iphone objective-c xcode ios ios4


    【解决方案1】:

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

    【讨论】:

    • 您好,感谢您的帮助,但将 errorView 委托从 self 更改为 nil 并没有解决问题。您能否向我解释一下如何执行这两种解决方案中的任何一种?再次非常感谢。
    • 我认为这可能是 Reachability 类中的某个东西,它发送了 3 次通知,可能在 3 个不同的地方。你知道它为什么会这样吗?无论如何,我认为解决方案可能是从 Reachability 类中删除我不想要的两个调用,但我不知道如何。你能帮忙吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-24
    • 1970-01-01
    相关资源
    最近更新 更多