我想在确认之前确认URL是可访问/可访问的。我知道reachabilityWithHostName
方法可以帮助我完成这项任务。我使用以下代码来了解URL可访问性。但它不起作用。
Reachability *connection = [Reachability reachabilityWithHostName:@"stackoverflow.com"];
[connection startNotifier];
NetworkStatus netStatus = [connection currentReachabilityStatus];
netStatus
总是会返回NotReachable
(意味着0
)值,即使一切都很好。
备注:
Reachability
课程
viewDidLoad
方法netStatus
会返回正确的值吗? ?!?!?!需要帮助!!混淆!!
答案 0 :(得分:1)
像这里使用可达性:https://github.com/tonymillion/Reachability
Reachability *connection = [Reachability reachabilityWithHostName:@"stackoverflow.com"];
connection.reachableBlock = ^(Reachability*reach)
{
dispatch_async(dispatch_get_main_queue(), ^{
NetworkStatus netStatus = [reach currentReachabilityStatus];
NSLog(@"network status: %ld", netStatus);
});
};
connection.unreachableBlock = ^(Reachability*reach)
{
NetworkStatus netStatus = [reach currentReachabilityStatus];
NSLog(@"network status: %ld", netStatus);
};
[connection startNotifier];