ASIHTTPRequest可达性如何

Mai*_*elS 4 iphone asihttprequest

尽管我喜欢ASIHTTPRequest,但在任何地方都没有记录如何使用修改后的Reachability类,我无法在stackoverflow或任何示例项目中找到它.

目前即时通讯:

Reachability *reach = [Reachability reachabilityWithHostName:@"http://google.com"];
[reach startNotifier];

if ([reach isReachable]) {
    NSLog(@"connection");
}else{
    NSLog(@"no connection");
}
Run Code Online (Sandbox Code Playgroud)

哪个似乎不起作用.

Jac*_*kin 6

您需要为此设置通知处理程序:

Reachability *reach = [Reachability reachabilityWithHostName:@"http://google.com"];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(reachabilityChanged:)
                                             name:kReachabilityChangedNotification
                                           object:nil];
[reach startNotifier];
Run Code Online (Sandbox Code Playgroud)

然后,像这样实现处理程序:

- (void) reachabilityChanged:(Reachability *) reach {
    if ([reach isReachable]) {
       NSLog(@"connection");
    } else{
       NSLog(@"no connection");
    }
}
Run Code Online (Sandbox Code Playgroud)

此外,当您不需要知道什么时候发生变化时,请以观察者身份移除自己:

[[NSNotificationCenter defaultCenter] removeObserver:self name:kReachabilityChangedNotification object:nil];
Run Code Online (Sandbox Code Playgroud)