【问题标题】:Xcode - Implement network error alerts into my app? Cant get it to work?Xcode - 在我的应用程序中实现网络错误警报?不能让它工作吗?
【发布时间】:2011-01-22 20:15:48
【问题描述】:

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

在我的 viewcontroller.h 文件中,我在“@interface”之前添加了“@class 可达性”,并且还添加了“- (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:' 的方法定义:'未找到”

这两个错误是: -“'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

【问题讨论】:

    标签: objective-c xcode ios ios4


    【解决方案1】:

    }s 在checkNetworkStatus: 的末尾有一个太多的}s。如果你的代码有适当的空白,那么犯这种错误就会困难得多。通过“适当的空白”,我的意思是你的代码应该是这样的:

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

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

    【讨论】:

    • 感谢您的帮助 :) 但我仍然收到完全相同的错误和警告
    • 也许你复制了我的源代码,就像我在回答中所说的那样?错误仍在我为您重新格式化的源代码中 - 如果您没有,请删除最后一个 }
    • 你怎么知道的?我删除了最后一个},但现在我得到了 3 个错误和警告,其中 2 个与以前相同的错误和警告,但在代码的最后还有另一个新的警告和错误。输入结束时的预期声明或语句,实现上下文中缺少“@end”。
    • 嘿!在checkNetworkStatus: 之前,您缺少},并且在它之后有一个太多(过去在粘贴时它被移到后面的那个)。我更新了原始答案中的代码,因此如果您将其粘贴到您的 checkNetworkStatus: 中,那么一切都会好起来的。
    • 我现在遇到了很多错误:/ 在非结构或联合 x3 中请求成员“internetActive”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-11
    • 1970-01-01
    • 2017-09-24
    相关资源
    最近更新 更多