简体   繁体   中英

How do I restrict access to UIViewController's that rely on an internet connection?

I need to restrict access to pages in my app that rely on an internet connection. Several of my pages have code that help the app communicate with an external database.

When an internet connection isn't available the pages don't behave as expected because there are certain checks that are done with the database that determine how certain things are displayed on the page.

Is there a way to detect there is no internet connection and when there isn't automatically have any page that tries to connect to the internet/grab data from an external database restricted?

Maybe show some "Currently out of order please try again later" page or something along those lines?

Thanks for your time.

Yes, as all says use reachability for checking of internet connection. download .h and .m file code from here and add in your project.

then in your .h file

#import "Reachability.h"
@interface ViewController : UIViewController
{
    NetworkStatus netStatus;
    Reachability *reach;
}

and in your .m file

reach = [Reachability reachabilityForInternetConnection];
netStatus = [reach currentReachabilityStatus];
if (netStatus != NotReachable)
{
     //present your view which needs internet connection.
}
else
{
     //show alert or message "Currently out of order please try again later"
}

As others have said already, using the reachability class is probably what you want to do. If you are already using a library like AFNetworking for your networking then it provides a reachability class of its own.

Moreover, the way I would handle this is to have each of your view controllers listen for reachability notifications and if there is no internet then disable the interaction with user elements that would take the user to a different view controller that requires internet connectivity while displaying a 'toast' notification below the navigation bar. That way the user is aware of the need for internet connectivity and arguably presents a better user experience.

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