简体   繁体   中英

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. 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..

I created a boolean variable hasInternet in app delegate but can't seem to access it elsewhere. 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). 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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