如何在iPhone中执行在线连接验证

时间:2011-06-16 10:04:29

标签: iphone objective-c

我在webserver中有两个数据库一个数据库,在本地有两个数据库 如果在线连接打开,我必须在设备面颊然后数据应该来自网络服务器数据库如果在线关闭然后数据来自sqlite本地数据库和iphone上的diplay数据请帮我解决这个问题

3 个答案:

答案 0 :(得分:1)

在你的appDelegate头文件

NetworkStatus remoteHostStatus;
NetworkStatus internetConnectionStatus;
NetworkStatus localWiFiConnectionStatus;
@property NetworkStatus remoteHostStatus;
@property NetworkStatus internetConnectionStatus;
@property NetworkStatus localWiFiConnectionStatus;
在您的appDelegate .m文件中

- (void)updateStatus
{
    self.remoteHostStatus           = [[Reachability sharedReachability] remoteHostStatus];
    self.internetConnectionStatus   = [[Reachability sharedReachability] internetConnectionStatus];
    self.localWiFiConnectionStatus  = [[Reachability sharedReachability] localWiFiConnectionStatus];
}

- (BOOL)isCarrierDataNetworkActive
{
    return (self.remoteHostStatus == ReachableViaCarrierDataNetwork);
}

- (NSString *)hostName
{
    return @"www.apple.com";
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{  
    [[Reachability sharedReachability] setHostName:[self hostName]];
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(reachabilityChanged:) 
                                                 name:@"kNetworkReachabilityChangedNotification" 
                                               object:nil];
}

答案 1 :(得分:1)

将remhabilty.m和reachabilty.h添加到您的项目中。在程序的整个生命周期中处于活动状态的控制器中,添加以下行并使用下面的回调。

[[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.apple.com"] retain];
    [hostReachable startNotifier];

回调如下:

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

    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)

    {
        case NotReachable:
        {
            break;

        }
        case ReachableViaWiFi:
        {
            break;

        }
        case ReachableViaWWAN:
        {
            break;

        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)

    {
        case NotReachable:
        {
            break;

        }
        case ReachableViaWiFi:
        {
            break;

        }
        case ReachableViaWWAN:
        {
            break;

        }
    }
}

答案 2 :(得分:0)

将可访问性用于网络相关信息。请参阅apple的可达性样本。