Internet连接在ios应用程序中每秒检查一次

时间:2016-08-18 05:26:20

标签: ios objective-c internet-connection

我需要在我的应用程序的每一秒检查互联网连接。我在谷歌搜索一小段搜索后有很多方法来执行此任务。请提供建议,这是在应用程序中检查互联网连接的最佳方式。

4 个答案:

答案 0 :(得分:2)

不是每隔n秒检查一次网络是否可以访问,而是更好的方法是观察可达性的变化。

您可以直接使用Apple API执行此操作(请参阅Apple开发站点上的the Reachability sample project),但使用内置于您正在使用的任何网络库中的可访问性功能会更容易。< / p>

尝试:

答案 1 :(得分:0)

在AFNetworking中使用AFNetworkReachabilityManager:

AFNetworking Reachability会不断检查网络连接

-(void)viewDidLoad {

    [super viewDidLoad];

[[AFNetworkReachabilityManager sharedManager] startMonitoring];

}

//Use the return value to check the Internet connection
-(BOOL)connected {

    return [AFNetworkReachabilityManager sharedManager].reachable;
}

答案 2 :(得分:0)

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

-(BOOL)reachabilityChanged:(NSNotification*)note
{
    BOOL status =YES;
    NSLog(@"reachability is changed");

    Reachability * reach = [note object];

    if([reach isReachable])
    {
        status = YES;
        NSLog(@"NetWork is Available. Please go ahead");
    }
    else
    {
        status = NO;
        NSLog(@"NetWork is not Available. Please check your connection.");
    }
    return status;
}

答案 3 :(得分:0)

相关问题