我已经将苹果的可达性(.h + .m)文件下载并添加到了我的视图基础项目中,但未进行任何更改。我还添加了systemconfiguration.framework。
在我的viewcontroller.h文件中,我在“@interface”之前添加了“@class可达性”,并且还添加了“-(无效)checkNetworkStatus:(NSNotification *)notice;”。就在“@end”之前。
我已经将reachability.h导入到我的viewcontroller.m文件中,其余部分如下:
//检查互联网连接
[[NSNotificationCenter defaultCenter] addObserver:自我选择器:@selector(checkNetworkStatus :)名称:kReachabilityChangedNotification对象:无];
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
最佳答案
在}
的末尾有太多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;
}
}
}
这种格式使查找包围/嵌套错误变得很简单。 :)