Xcode - 在我的应用程序中实现网络错误警报?不能让它工作?

时间:2011-01-22 20:15:48

标签: objective-c xcode ios ios4

我已下载苹果可访问性(.h + .m)文件并将其添加到我的视图库项目中,并未进行任何更改。我还添加了systemconfiguration.framework。

在我的viewcontroller.h文件中,我在“@interface”之前添加了“@class reachability”,我还添加了“ - (void)checkNetworkStatus:(NSNotification *)notice”。就在“@end”之前。

我已将reachability.h导入到我的viewcontroller.m文件中,以下是其余部分:

  

//检查互联网连接           [[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

- (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;       
            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)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)dealloc:(BOOL)animated
{       
[[NSNotificationCenter defaultCenter] removeObserver:self];

    [super dealloc];
}

@end

我得到2个警告: - “类'TestViewController'的实现不正确”(在我尝试这样做之前有效) - 还有,“-checkNetworkStatus的方法定义:'未找到'

2个错误是: - “'checkNetworkStatus'未声明” - 并“预期”;'在':'之前

有人可以帮助我吗?

编辑:

.h文件:

>#import <UIKit/UIKit.h>

@class Reachability;

@interface TestViewController : UIViewController {

IBOutlet UIView *landscape;
IBOutlet UIView *portrait;
IBOutlet UIView *portraitupsidedown;

IBOutlet UIWebView *WebView;
IBOutlet UIWebView *WebView2;
IBOutlet UIWebView *WebView3;
IBOutlet UIWebView *WebView4;

Reachability* internetReachable;
    Reachability* hostReachable;
}

@property(nonatomic,retain) UIView *landscape;
@property(nonatomic,retain) UIView *portrait;
@property(nonatomic,retain) UIView *portraitupsidedown;

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

@end

1 个答案:

答案 0 :(得分:2)

}结尾处有一个太多checkNetworkStatus:秒。如果你的代码是正确的白色间隔,那么犯这种错误会更加困难。通过“正确的白色间距”我的意思是你的代码应该是这样的:

}
- (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;       
            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;              
        }
    }
}

这种格式化使得查找包围/嵌套错误变得微不足道。 :)