2

In my application i want to know current status for Airplane Mode. I gone through many links, some them said that we can use reachability to know airplane mode status, but not getting proper results. And same way to know the status for Airplane Mode by Using Private Framework: Importing RadioPreferences.h, but some one said that by using this we can not submit app to Appstore. But i have to submit app to appstore

Following are the some of reference links which i followed Reachability airplane mode (3G) vs. Wifi Using Private Framework: Importing RadioPreferences.h

Community
  • 1
  • 1
Anjaneyulu Battula
  • 1,910
  • 16
  • 33

1 Answers1

0

Hi created my Own methods to check

#import <Foundation/Foundation.h>
#import "AppDelegate.h"
@interface InternetConnectionCheck : NSObject

+(BOOL)CheckConnection ;

@end


#import "InternetConnectionCheck.h"
#import <SystemConfiguration/SystemConfiguration.h>

@implementation InternetConnectionCheck

static InternetConnectionCheck *singletonObject = nil;


+ (id) sharedInstance
{
    if (! singletonObject) {
        singletonObject = [[InternetConnectionCheck alloc] init];
    }
    return singletonObject;
}


// Initialize class object
- (id)init
{
    if (! singletonObject) {
        singletonObject = [super init];
    }
    return singletonObject;
}

+(BOOL)CheckConnection{
    NSString * str = [[InternetConnectionCheck sharedInstance] newtworkType];
    if ( [[InternetConnectionCheck sharedInstance] connectedToInternet]==YES) {
        NSLog(@"INTERNET AVAILABLE VIA %@", str);
        return YES;
    }else{
        NSString * msg = [NSString stringWithFormat:@"Device is Connected Via %@ But We Unable to reach",str];
        UIAlertView * alert =[[UIAlertView alloc]initWithTitle:@"Error" message:msg delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        [alert show];
        return NO;
    }
    return NO;
}


-(NSString * )newtworkType {
    NSArray *subviews = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"]subviews];
    NSNumber *dataNetworkItemView = nil;

    for (id subview in subviews) {
        if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
            dataNetworkItemView = subview;
            break;
        }
    }

    NSString * connectedBy =@"None";
    switch ([[dataNetworkItemView valueForKey:@"dataNetworkType"]integerValue]) {
        case 0:
            NSLog(@"No wifi or cellular");
            connectedBy=@"No wifi or cellular";
            break;

        case 1:
            NSLog(@"2G");
            connectedBy= @"2G";
            break;

        case 2:
            NSLog(@"3G");
            connectedBy= @"3G";
            break;

        case 3:
            NSLog(@"4G");
            connectedBy= @"4G";
            break;

        case 4:
            NSLog(@"LTE");
            connectedBy= @"LTE";
            break;

        case 5:
            NSLog(@"Wifi");
            connectedBy=@"Wifi";
            break;


        default:
            break;
    }
    return connectedBy;
}

- (BOOL)connectedToInternet
{
    NSString *urlString = @"http://www.google.com/";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url
                                                                                             cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                                                         timeoutInterval:5.0];

    NSHTTPURLResponse *response;
    NSURLResponse * resp;

    [self sendSynchronousRequest:request returningResponse:&resp error:nil];

    if ([resp isKindOfClass:[NSHTTPURLResponse class]]) {
        response = (NSHTTPURLResponse*)resp;
    }

    return ([response statusCode] == 200) ? YES : NO;

}

- (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error
{

    NSError __block *err = NULL;
    NSData __block *data;
    BOOL __block reqProcessed = false;
    NSURLResponse __block *resp;

    [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable _data, NSURLResponse * _Nullable _response, NSError * _Nullable _error) {
        resp = _response;
        err = _error;
        data = _data;
        reqProcessed = true;
    }] resume];

    while (!reqProcessed) {
        [NSThread sleepForTimeInterval:0];
    }

    *response = resp;
    return data;
}

@end

And Use it Like

 // Check Connection.
    BOOL Y =  [InternetConnectionCheck CheckConnection];
    NSLog(@"bool %s", Y ? "true" : "false");

For Testing it on Simulator you can check my Blog

Sagar Shirbhate
  • 801
  • 7
  • 17