Iphone ASIHTTPRequest可达性如何
尽管我很喜欢ASIHTTPRequest,但它并没有记录如何使用修改后的可达性类,我在stackoverflow或任何示例项目中也找不到它 目前我在这一点上:Iphone ASIHTTPRequest可达性如何,iphone,asihttprequest,Iphone,Asihttprequest,尽管我很喜欢ASIHTTPRequest,但它并没有记录如何使用修改后的可达性类,我在stackoverflow或任何示例项目中也找不到它 目前我在这一点上: Reachability *reach = [Reachability reachabilityWithHostName:@"http://google.com"]; [reach startNotifier]; if ([reach isReachable]) { NSLog(@"connection"); }else{
Reachability *reach = [Reachability reachabilityWithHostName:@"http://google.com"];
[reach startNotifier];
if ([reach isReachable]) {
NSLog(@"connection");
}else{
NSLog(@"no connection");
}
这似乎不起作用 您需要为此设置通知处理程序:
Reachability *reach = [Reachability reachabilityWithHostName:@"http://google.com"];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name:kReachabilityChangedNotification
object:nil];
[reach startNotifier];
然后,像这样实现处理程序:
- (void) reachabilityChanged:(Reachability *) reach {
if ([reach isReachable]) {
NSLog(@"connection");
} else{
NSLog(@"no connection");
}
}
此外,当您不需要知道事情何时发生变化时,请将自己作为观察者:
[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
}
,请进行这些更改,然后代码将像一个符咒一样工作:)
可达性
最初是由苹果公司在其示例代码中提供的一个类,在我看来这是一个很好的文档。可以在这里找到:当可达性实际发生更改时,未调用reachabilityChanged。@MaikelS这可能是因为reach
为零。这样做:Reachability*reach=[[Reachability-Reachability-whithhostname:@“google.com”]retain]
并在dealloc或适当的位置添加一个版本这实际上是正确的处理程序。如果使用圆弧,请添加属性以使其保持活动状态。
- (void) reachabilityChanged:(NSNotification *)notification
{
Reachability *localReachability = [notification object];
if ([localReachability isReachable])
{
NSLog(@"connection");
} else{
NSLog(@"no connection");
}