可以使用或不提供连续检查网络连接

时间:2014-03-27 12:34:01

标签: ios ios7 reachability

我已经在我的项目中实现了覆盖能力,在执行任何网络连接任务时,在某个特定时间检查连接是否可达。但我想要的是连续检查网络是否可以显示通知但如何实现它我不知道所以请帮助

3 个答案:

答案 0 :(得分:5)

使用此代码检查设备中是否有网络连接使用可达性

@interface appDelegate : UIResponder <UIApplicationDelegate>
{
    Reachability *internetReachable;
}

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

 internetReachable = [Reachability reachabilityForInternetConnection];
    [internetReachable startNotifier];
 [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(checkNetworkStatus:)
                                                 name:kReachabilityChangedNotification object:nil];
............

}

- (void)checkNetworkStatus:(NSNotification *)notice {
    // called after network status changes

    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)
    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");
            break;
        }
        case ReachableViaWiFi:
        {
             NSLog(@"The internet is Connected.");
            break;
        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN!");
            break;
        }
    }
}

//#import "Reachability.m"

static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
{
#pragma unused (target, flags)
    NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback");
    NSCAssert([(__bridge NSObject*) info isKindOfClass: [Reachability class]], @"info was wrong class in ReachabilityCallback");

    Reachability* noteObject = (__bridge Reachability *)info;
    // Post a notification to notify the client that the network reachability changed.
    [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject];
}

it's Solve your problem.

答案 1 :(得分:0)

只需在您的App Delegate中调用:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self testInternetConnection];

    return YES;
}   

- (void)testInternetConnection
{
    internetReachability = [Reachability reachabilityWithHostname:@"www.google.com"];

    UIAlertView *alertForInternet = [[UIAlertView alloc]initWithTitle:@"Internet Connection Established." message:Nil delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];

    internetReachability.reachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            [alertForInternet setTitle:@"Internet Connection Established."];
            [alertForInternet setMessage:Nil];
            [alertForInternet show];


        });
    };

    // Internet is not reachable
    internetReachability.unreachableBlock = ^(Reachability*reach)
    {
        // Update the UI on the main thread
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Someone broke the internet :(");
            [alertForInternet setTitle:@"Internet Connection Lost."];
            [alertForInternet setMessage:@"Please Check the Connection"];
            [alertForInternet show];

        });
    };

    [internetReachability startNotifier];
}

这里

[internetReachability startNotifier];会照顾其余部分。

答案 2 :(得分:-4)

使用计时器。设置计时器以小间隔检查连接可用性,可能是1秒。您检查此类活动的方式通常是使用计时器。其他选择是找到一个通知已发生此事件的事件。