我有一个初始tableviewcontroller,它正在执行可达性检查。这在viewDidLoad
,然而中没有问题。我想知道重试连接的正确方法,直到它通过。我的实现文件中的相关代码如下所示,如果连接断开,我尝试插入[self ViewDidLoad]
,但这只是将应用程序设置为循环(返回连接失败NSLog
消息)而不显示{ {1}}。
UIAlertView
答案 0 :(得分:3)
您应该如何使用Reachability?
要接收通知,请注册通知,然后从Apple启动可访问性类:
@implementation AppDelegate {
Reachability *_reachability;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(reachabilityChanged:)
name: kReachabilityChangedNotification
object: nil];
_reachability = [Reachability reachabilityWithHostName: @"www.apple.com"];
[_reachability startNotifier];
// ...
}
@end
回答通知:
- (void) reachabilityChanged: (NSNotification *)notification {
Reachability *reach = [notification object];
if( [reach isKindOfClass: [Reachability class]]) {
}
NetworkStatus status = [reach currentReachabilityStatus];
NSLog(@"change to %d", status); // 0=no network, 1=wifi, 2=wan
}
如果您更喜欢使用块,请使用KSReachability。