Iphone Sdk在应用程序中间自动检查互联网连接?

Iphone Sdk在应用程序中间自动检查互联网连接?,iphone,sdk,connection,Iphone,Sdk,Connection,当我第一次启动应用程序时,我目前正在检查互联网连接,但如果我关闭应用程序并打开飞行模式,然后运行应用程序恢复工作,或者如果我失去wifi,它应该检查互联网连接,它应该说你的互联网连接丢失或诸如此类的事情。有人对此有正确的答案吗?使用可达性API。下面是我的一个项目的应用程序代理文件中的一些代码 // ivars Reachability* hostReach; Reachability* internetReach; Reachability* wifiReach; - (void) reac

当我第一次启动应用程序时,我目前正在检查互联网连接,但如果我关闭应用程序并打开飞行模式,然后运行应用程序恢复工作,或者如果我失去wifi,它应该检查互联网连接,它应该说你的互联网连接丢失或诸如此类的事情。有人对此有正确的答案吗?

使用可达性API。下面是我的一个项目的应用程序代理文件中的一些代码

// ivars
Reachability* hostReach;
Reachability* internetReach;
Reachability* wifiReach;

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

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

    if ([curReach currentReachabilityStatus] == NotReachable) {

        UIAlertView *alert = [[[UIAlertView alloc] init] autorelease];
        alert.title = @"No network connection?";
        alert.message = @"No network connection.";
        alert.delegate = self;
        [alert addButtonWithTitle:@"OK"];
        [alert show];

    } 

}

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

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

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

    internetReach = [[Reachability reachabilityForInternetConnection] retain];
    [internetReach startNotifier];

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

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

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

    return YES;
}

// and of course, remember to release all those resources..