简体   繁体   English

iOS / xcode / objective-c:创建会话变量以实现可达性

[英]iOS/xcode/objective-c: create session variable for reachability

I do a test in app delegate using the reachability class to see if there is an Internet connection. 我使用可达性类在应用程序委托中进行测试,以查看是否存在Internet连接。 I put this in app delegate since I need to check Internet access in many parts of the app at many points and thought app delegate is always running in background.. 我将其放在应用程序委托中,因为我需要在很多时候检查应用程序许多部分的Internet访问,并且认为应用程序委托始终在后台运行。

I created a boolean variable hasInternet in app delegate but can't seem to access it elsewhere. 我在应用程序委托中创建了一个布尔变量hasInternet,但似乎无法在其他地方访问它。 The following in another class throws error: 另一个类中的以下内容引发错误:

     if (hasInternet) {
//do something
        }

Is this the right approach to keeping track of reachability or how can I set a variable that can be checked throughout the entire app. 这是跟踪可达性的正确方法,还是如何设置可以在整个应用程序中检查的变量。

This is a valid option, but to use this variable you have to declare it as a property (possibly only read-only in .h, and read-write in .m). 这是一个有效的选项,但是要使用此变量,必须将其声明为属性 (可能仅在.h中为只读,而在.m中为读写)。 You can then access it in other classes with 然后,您可以在其他课程中使用

((AppDelegate *)[UIApplication sharedApplication].delegate).hasInternet

(where AppDelegate is the actual name of your app delegate class) (其中AppDelegate是您的应用程序委托类的实际名称)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM