开发者

How to perform validation of online connection in iphone

开发者 https://www.devze.com 2023-03-13 04:46 出处:网络
i have two database one database in webserver and other database in local here i have to cheek in device if online connection is on then data should come from webserver database 开发者_StackOverflow中

i have two database one database in webserver and other database in local here i have to cheek in device if online connection is on then data should come from webserver database 开发者_StackOverflow中文版if online is off then data is come from sqlite local database and diplay data on iphone please help me in this problem


Add reachabilty.m and reachabilty.h to your project. In your controller which is active throughout the life of the program add the following lines and use the below callback.

[[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];

The callback is as follows:

- (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;

        }
    }
}


In your appDelegate header file

NetworkStatus remoteHostStatus;
NetworkStatus internetConnectionStatus;
NetworkStatus localWiFiConnectionStatus;
@property NetworkStatus remoteHostStatus;
@property NetworkStatus internetConnectionStatus;
@property NetworkStatus localWiFiConnectionStatus;

in your appDelegate .m file

- (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];
}


Use Reachability for network related information. See apple's sample of reachability.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号