简体   繁体   中英

Restrict iOS app to wifi only?

I am looking for a way to restrict my iOS app (or even a portion of it ) to wifi only. I've looked through the reachability example and haven't really come up with a solution. I can get it to display a message when the user is connected to 3g, but I don't know how to get it to stop loading the view.

I have a view that loads another view when a button is pressed. I want that second view to close if the device is connected to 3g. How can I go about doing this?

I've never done what you're trying to do before, but I imagine it's just a matter of figuring out the Reachability API.

I'd start out with some code in your AppDelegate class:

// ivars
Reachability *wifiReach;
Reachability *hostReach;

- (void) reachabilityChanged: (NSNotification *)note {

    Reachability *curReach = (Reachability *)[note object];

    if ([curReach currentReachabilityStatus] == NotReachable) {
        // do something
    } 

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];

    hostReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
    [hostReach startNotifier];

    wifiReach = [[Reachability reachabilityForLocalWiFi] retain];
    [wifiReach startNotifier];

    // controller setup
    viewController = [[CFSplashViewController alloc] init];

    [window addSubview:viewController.view];
    [window makeKeyAndVisible];

    return YES;
}

It's a really simple piece of code. What you could do here is listen for changes in reachability status and then set up your app to react appropriately. You don't necessarily have to do this in your AppDelegate class. It all depends on what you want to accomplish.

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