任何人都可以建议在 wifi 丢失互联网连接时进行可达性检查的示例

Can any one suggest sample for Reachability check when wifi loss inter net connection

一切正常后,我已将可达性添加到我的项目中。 1.It 检查成功主机请求、wifi 或移动日期活动连接.. 但我已经测试了 wifi 在失去互联网连接的情况下的可达性,它可能会给出类似通过 wifi 可达的结果......(例如你有活动的 wifi 连接但没有从 wifi 接收到互联网)

我确实添加了 NSTimers 并取得了准确的结果,但我想通过可达性来实现这件事所以任何人都可以帮助解决我的问题...

我也有类似的问题来检测互联网是否丢失。示例:您已连接到 WIFI,但由于账单结算或其他原因,互联网未激活。

如果您使用的是 AFNetworking,如果您在执行某些 POST 请求时失去互联网连接,将会出现错误代码。

下面是我的代码。

 AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    [manager POST:@"" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {

        //If response Sucess

    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"Error %@",error);

        if(error.code==-1009){
            // This Error Comes when internet is not active.
so inside this you can make any action
        }


    }];

这是我的答案,

我没有仅使用完成块初始化计时器,而是延迟几乎等于 NSTIMERS 的完成块。

我已经创建了 api 以在所有条件下实现可达性..

只需附上示例方法即可检查所有条件..

-(void) blocksWithReachabiltyCheck :(bool) r_Status
{
    __weak id weakSelf = self;
    callBack = ^{

        double delayInSeconds = 10.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){

            id strongSelf = weakSelf;

            if (!strongSelf) {
                return;
            }
            // Schedule the timer again
            //callBack();
            [weakSelf targetMethod:r_Status];
        });

    };

    // Start the timer for the first time
    callBack();
}

-(void)targetMethod:(bool)sender
{
    NSString *remoteHostName = @"www.apple.com";
    // NSString *remoteHostLabelFormatString = NSLocalizedString(@"Remote Host: %@", @"Remote host label format string");
    //    self.remoteHostLabel.text = [NSString stringWithFormat:remoteHostLabelFormatString, remoteHostName];
    // NSLog(@"%@",remoteHostLabelFormatString);
    self.hostReachability = [Reachability reachabilityWithHostName:remoteHostName];
    [self.hostReachability startNotifier];


    self.internetReachability = [Reachability reachabilityForInternetConnection];
    [self.internetReachability startNotifier];


    self.wifiReachability = [Reachability reachabilityForLocalWiFi];
    [self.wifiReachability startNotifier];

    if (sender == YES)
    {
        callBack();
    }

}



  //stop timer
    -(void) RXStopNotifier
    {

        [self blocksWithReachabiltyCheck:NO];
    }
    //start notifier with host name
    -(void) RXStartNotifier:(NSString *)hostNameString
    {

        [self blocksWithReachabiltyCheck:YES];
        hostName  = hostNameString;
    }

@synthesize callBack;
@property (copy)__block void (^callBack)(void) ;

//Notification method
- (void) RXSeachabilityChanged:(NSNotification *)note
{
    if (timerFlag == false)
    {
        timerFlag = true;
        Reachability* curReach = [note object];
        NetworkStatus netStatus = [curReach currentReachabilityStatus];;
        statusReach = 0;

        switch (netStatus)
        {
            case NotReachable:        {
                NSLog(@"Not Access ");
                statusReach = 0;
                break;
            }

            case ReachableViaWWAN:
                //            {
                //                NSLog(@"Reachable WWAN");
                //                statusReach = 1;
                //                //            imageView.image = [UIImage imageNamed:@"WWAN5.png"];
                //                break;
                //            }
            case ReachableViaWiFi:        {
                if (instantFlag == NO)
                {

                    NSLog(@"Reachable WIFI or Reachable WWAN");
                    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:hostName] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:1];
                    NSURLResponse *response = nil;
                    NSError *error = nil;
                    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
                    //NSLog(@"response %d", [(NSHTTPURLResponse *)response statusCode]);
                    if ([(NSHTTPURLResponse *)response statusCode] == 200) {

                        statusReach = 1;
                        NSLog(@"Success");
                    }
                    else
                    {


                        statusReach = 2;
                        NSLog(@"Failed");
                    }
                }
                else
                {
                    statusReach = 1;
                }
                break;
            }
        }


    }
}

如果有人有疑问,请联系我....