gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 11:23:59 24 4
gpt4 key购买 nike

我已经下载了 apples reachability(.h+.m) 文件并将其添加到我的 View 基础项目中,并且没有进行任何更改。我还添加了 systemconfiguration.framework。

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

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

// 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

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

最佳答案

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

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

关于objective-c - Xcode - 在我的应用程序中实现网络错误警报?不能让它工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4770303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com