开发者

What is the simplest way to implement Reachability (No Internet popup) into an iOS App? [duplicate]

开发者 https://www.devze.com 2023-03-30 16:44 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: How to check for an active Internet Connection on iPhone SDK?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to check for an active Internet Connection on iPhone SDK?

What开发者_高级运维 is the simplest way to implement Reachability (Code that notifies the user that there is no Internet connection) into an iOS App?


I did this in my appDelegate

in appDelegate.h:

@interface myAppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet noInternetViewController *NoInternetViewController;
BOOL isShowingNoInternetScreen;
}

in appDelegate.m

//at top
#import "Reachability.h"

//somewhere under @implementation
-(void)checkInternetConnection {
Reachability *r = [Reachability reachabilityWithHostName:@"www.google.com"];

NetworkStatus internetStatus = [r currentReachabilityStatus];

if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
    if (!isShowingNoInternetConnectionScreen) {
        [self.navigationController pushViewController:NoInternetViewController animated:YES];
        isShowingNoInternetConnectionScreen = YES;
    }

}
else if (isShowingNoInternetConnectionScreen) {
    [self.navigationController popViewControllerAnimated:YES];
    isShowingNoInternetConnectionScreen = NO;
    }
}

//inside application:didFinishLaunchingWithOptions: or in application:didFinishLaunching
isShowingNoInternetConnectionScreen = NO;

[NSTimer scheduledTimerWithTimeInterval:3.0 target:self selector:@selector(checkInternetConnection) userInfo:nil repeats:YES];

I'm using a navigation controller, but if you aren't, then just change the pushViewController:animated: to presentModalViewController:animated: and change popViewControllerAnimated: to dismissModalViewController

You'll obviously need a view controller set up in Interface Builder that is tied to the IBOutlet as well. Just make this whatever you want the user to see when they have no connection.

0

精彩评论

暂无评论...
验证码 换一张
取 消