Tuesday, December 15, 2015

DataManager Classes For iOS

  DataManager.h

//
//  DataManager.h
//

#import <Foundation/Foundation.h>
#import "Reachability.h"
@interface DataManager : NSObject
{
    Reachability* hostReach;
    Reachability* internetReach;
    Reachability* wifiReach;

}
#pragma mark Singleton Methods
@property(nonatomic,assign)BOOL isConnectionReachable;

+ (id)sharedManager;
-(void)checkForReachability;
+ (BOOL) checkInternet;


@end

--------------------------------------------------------------------------------------------------------------
                                                               DataManager.m

//
//  DataManager.m
//

#import "DataManager.h"
static DataManager *sharedMyManager = nil;

@implementation DataManager
#pragma mark Singleton Methods
@synthesize isConnectionReachable;
+ (id)sharedManager {
    
    
    @synchronized(self) {
        if(sharedMyManager == nil)
            sharedMyManager = [[super allocWithZone:NULLinit];
        [sharedMyManager checkForReachability];
    }
    return sharedMyManager;
}

+ (id)allocWithZone:(NSZone *)zone {
    return [self sharedManager] ;
}

- (id)copyWithZone:(NSZone *)zone {
    return self;
}

// Internet Connectiong Checking Method.
+ (BOOL) checkInternet{
    
    //Test for Internet Connection
    NSString *hostName = @"www.google.com";
    Reachability *r = [Reachability reachabilityWithHostName:hostName];
    
    NetworkStatus internetStatus = [r currentReachabilityStatus];
    
    BOOL internet;
    
    if ((internetStatus != ReachableViaWiFi) && (internetStatus != ReachableViaWWAN)) {
        internet = NO;
    }
    else {
        internet = YES;
    }
    return internet;
}
#pragma mark reachbility code
-(void)checkForReachability
{
    /////////////Reachbility
    
    
    
    [[NSNotificationCenter defaultCenteraddObserverself selector:@selector(reachabilityChanged:) namekReachabilityChangedNotification objectnil];
    
    
    //Change the host name here to change the server your monitoring
    
    hostReach = [Reachability reachabilityWithHostName@"www.google.co.in"] ;
    [hostReach startNotifier];
    
    
    internetReach = [Reachability reachabilityForInternetConnection];
    [internetReach startNotifier];
    
    
    wifiReach = [Reachability reachabilityForLocalWiFi] ;
    [wifiReach startNotifier];
   
    isConnectionReachable=[self isInternetReachable];
}
//Called by Reachability whenever status changes.

- (void) reachabilityChanged: (NSNotification* )note
{
    Reachability* curReach = [note object];
    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
    isConnectionReachable=![self isInternetReachable];
}

- (BOOL)isInternetReachable
{
    struct sockaddr_in zeroAddress;
    bzero(&zeroAddress, sizeof(zeroAddress));
    zeroAddress.sin_len = sizeof(zeroAddress);
    zeroAddress.sin_family = AF_INET;
    
    SCNetworkReachabilityRef reachability =SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const structsockaddr*)&zeroAddress);
    SCNetworkReachabilityFlags flags;
    
    if(reachability == NULL)
        return false;
    
    if (!(SCNetworkReachabilityGetFlags(reachability, &flags)))
        return false;
    
    if ((flags & kSCNetworkReachabilityFlagsReachable) == 0)
        // if target host is not reachable
        return false;
    
    
    BOOL isReachable = false;
    
    
    if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0)
    {
        // if target host is reachable and no connection is required
        //  then we'll assume (for now) that your on Wi-Fi
        isReachable = true;
    }
    
    
    if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) ||
         (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0))
    {
        // ... and the connection is on-demand (or on-traffic) if the
        //     calling application is using the CFSocketStream or higher APIs
        
        if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0)
        {
            // ... and no [user] intervention is needed
            isReachable = true;
        }
    }
    
    if ((flags & kSCNetworkReachabilityFlagsIsWWAN) ==kSCNetworkReachabilityFlagsIsWWAN)
    {
        // ... but WWAN connections are OK if the calling application
        //     is using the CFNetwork (CFSocketStream?) APIs.
        isReachable = true;
    }
    return isReachable;
}

@end

how to check whether internet connection is available or not in ios


 if (dataManager.isConnectionReachable) {
            [SVProgressHUD showWithStatus:@"Loading.."];
            [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(postDataOnWebserviceForUpdatePassword) userInfo:nil repeats:NO];
            // [self postDataOnWebserviceForLogin];
        }else{
            showAlert(@"Check Connection", @"Internet is not available.");
        }


No comments:

Post a Comment