简体   繁体   English

如何使用可达性来检查是否存在Internet连接,如果没有,请执行某些操作。 (基本上用作布尔值。)

[英]How do I use Reachability to check if there's an internet connection, and if not, do something. (Basically use as a boolean.)

I've read over the code on Reachability 's page, but I'm unclear as to how I'd use it in an if statement type scenario. 我已经阅读了Reachability页面上的代码,但是不清楚在if语句类型场景中如何使用它。

For example, the user taps the refresh button, firing the refresh method (go figure, eh?) and if there is an internet connection, I want the refresh of data to occur, but if not, I want to send a notification of no internet. 例如,用户点击刷新按钮,触发refresh方法(如图所示,是吗?),如果存在互联网连接,我希望刷新数据,但是如果没有,我要发送否通知互联网。

How would I achieve something like this with Reachability? 如何达到可触及性?

Would I just use code like the following but have a variable set to a value in each block depending on the result, and just look at it after. 我将只使用以下代码,但根据结果在每个块中将一个变量设置为一个值,然后再对其进行查看。 That seems less than elegant, but if it's the only way, so be it. 这似乎不那么优雅,但是如果这是唯一的方法,那就去吧。

// allocate a reachability object
Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"];

// set the blocks 
reach.reachableBlock = ^(Reachability*reach)
{
    NSLog(@"REACHABLE!");
};

reach.unreachableBlock = ^(Reachability*reach)
{
    NSLog(@"UNREACHABLE!");
};

// start the notifier which will cause the reachability object to retain itself!
[reach startNotifier];

My app did just that - send a message in the block to self on the main thread - say hostChanged:(BOOL)state. 我的应用程序就是这样做的-在块中向主线程中的self发送一条消息-说hostChanged:(BOOL)状态。 Your self class can then maintain a public ivar of the state, or and a notification other objects can listen for. 然后,您的自类可以维护状态的公共变量,或者其他对象可以侦听的通知。

My app had this function in the app delegate so it was easy for other objects to get to it. 我的应用程序在应用程序委托中具有此功能,因此其他对象很容易到达它。

Note that it takes iOS tens of seconds to determine the state, so my code launched with he assumption of UP. 请注意,iOS需花费数十秒钟来确定状态,因此我的代码是在假设UP的情况下启动的。

-(BOOL)NetworkConnectionCheck {
    Reachability *reachability = [Reachability reachabilityForInternetConnection];
    return [reachability isReachable];                                                                                                                                               
}

-(void)viewDidLoad {
if (!self.NetworkConnectionCheck) { // checks active network connection
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"There are no active wifi or data connection!" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
            [alert show];           
 } else {
       // do your refresh stuff here......
}

Quick way to check network connection. 快速检查网络连接的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在xcode中使用可达性来评估Internet连接 - How to use Reachability in xcode for assessment Internet Connection 如何使用可达性类来检测有效的Internet连接? - How to use reachability class to detect valid internet connection? Swift 3通过可访问性检查互联网连接 - Swift 3 check internet connection with Reachability 如何在没有可达性类的情况下检查iPhone中的互联网连接? - How to check the internet Connection in iPhone without Reachability classes? 如何限制对依赖互联网连接的UIViewController的访问? - How do I restrict access to UIViewController's that rely on an internet connection? 如何使用 iOS 可达性 - How to use iOS Reachability 如何使用 ASIHTTPRequest 可达性 - how to use ASIHTTPRequest reachability 如何检查WatchOS 2连接(可达性)到互联网? - How check WatchOS 2 connected (reachability) to the internet? 我是否必须做一些特别的事情才能让 TestFlight 构建使用我的通知扩展目标? 它被忽略 - Do I have to do something special to have TestFlight builds use my Notification Extension target? It's ignored AFNetworking中可达性的用途是什么? - What's the use of reachability in AFNetworking?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM