一旦丢失,iPhone 4多久会搜索一次数据服务?

时间:2011-10-03 17:42:47

标签: iphone ios reachability

长时间读者,第一次问问。

我正在编写一个iPhone应用程序,需要处理进出数据覆盖范围的电话,并且有一些优雅。我可以通过通知设置Reachability,以找出丢失或返回的时间,但是我知道无线电有多久寻找信号会有所帮助 - 并且这个速率会随着时间的推移而减慢吗?另外,有什么我可以编程的方式(比如在我知道我没有覆盖时ping服务器)来加速它吗?

电池寿命对我来说并不是一个大问题,我不会通过iTunes进行部署。

1 个答案:

答案 0 :(得分:1)

你想要什么是可能的。首先获得Reachability code from Apple。然后,您需要编写checkNetworkStatus实现。这是通知的来源 -

#import "Reachability.h"

- (void)checkNetworkStatus:(NSNotification *)notice
{
    // called after network status changes
    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];

    switch(internetStatus)
    {
        case NotReachable:
        {
            self.internetActive = NO;
            break;
        }
        case ReachableViaWiFi:
        {
            self.internetActive = YES;
            break;
        }
        case ReachableViaWWAN:
        {
            self.internetActive = YES;
            break;
        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)
    {
        case NotReachable:
        {
            self.hostActive = NO;
            break;
        }
        case ReachableViaWiFi:
        {
            self.hostActive = YES;
            break;
        }
        case ReachableViaWWAN:
        {
            self.hostActive = YES;
            break;
        }
    }
    return;
}

现在您需要开始发送通知 -

-(void)viewWillAppear:(BOOL)animated
{
    //NSLog(@"View Will Appeared!!");

    // check for internet connection
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(checkNetworkStatus:) 
                                                 name:kReachabilityChangedNotification 
                                               object:nil];
    internetReachable = [[Reachability reachabilityForInternetConnection] retain];
    [internetReachable startNotifier];

    // check if a pathway to a random host exists
    hostReachable = [[Reachability reachabilityWithHostName: @"www.google.com"] retain];
    [hostReachable startNotifier];

    // now patiently wait for the notification
    return;
}
相关问题