当wifi丢失网络连接时,任何人都可以建议用于可达性检查的样本

时间:2016-03-21 06:33:03

标签: ios objective-c reachability

在事情正常后,我已将可达性添加到我的项目中。 1.检查成功主机请求,wifi或移动日期活动连接.. 但我已经测试了无线网络的可达性与互联网连接的丢失,它可能会给出通过无线网络可以获得的结果...(例如你有活跃的wifi连接,但没有收到互联网接收形式wifi)

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

2 个答案:

答案 0 :(得分:0)

我也有类似的问题来检测互联网是否丢失。例如:您已连接到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
        }


    }];

答案 1 :(得分:0)

这是我的答案,

我没有使用完成块初始化定时器,但是延迟几乎等于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;
            }
        }


    }
}

如果有任何怀疑的人到达我那里......