【发布时间】:2011-01-27 20:51:53
【问题描述】:
好的,我正在尝试让这个应用程序显示网络错误警报代码。我添加了 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 次,而我显然只希望它显示一次。
我认为这可能是 Reachability 类中的某个东西,它发送了 3 次通知,可能在 3 个不同的地方。我不知道它为什么这样做,但我认为解决方案可能是从 Reachability 类中删除我不想要的两个调用,但我不知道如何。
有人可以帮帮我吗? 提前致谢。
编辑:
8个警告和4个错误如下:
“TestViewController”可能不会响应“+checkConnectivity”
初始化从没有强制转换的指针生成整数
“连接”的本地声明隐藏了实例变量
“MyReachability”未声明
“可达性”未声明
“TestViewController”可能不会响应“-siteAvailable”
'TestViewController' 可能不会响应'-addConnectivityView'
重新定义'-[TestViewControllerreachabilityChanged:]'
“MyReachability”未声明
TestViewController 可能无法响应“-siteAvailable”
TestViewController 可能无法响应“-addConnectivityView”
TestViewController 可能无法响应“-removeConnectivityView”
【问题讨论】:
-
如果您正确阅读了问题(不是标题本身),您会发现最终我正文底部的最后一个问题略有不同。我提出的最后一个问题已经在一定程度上得到了回答,并且问题已经继续发展,变得更加成熟。
-
此外,关于我之前措辞相似的问题,一些非常友善和乐于助人的人已经回答了,我已经评论了他们的回答以深入研究我的问题,但他们没有回答。
-
[[NSNotificationCenter defaultCenter] postNotificationName:@"kReachabilityChangedNotification" object: nil];在哪里?请显示所有代码... -
它在 Apples Reliability.m 代码中,如果你愿意,我可以发布它吗?
标签: iphone objective-c xcode ios ios4