Reevanshi Garg
Reevanshi Garg

Reputation: 11

How to check celluar/wifi internet strength in Objective c?

I am developing an iot app ,I want to show a pop-up in case wifi/celluar internet is slow . How to implement the slow networks alert in objective c ?

Upvotes: 0

Views: 861

Answers (3)

Dishant Rajput
Dishant Rajput

Reputation: 1357

Full example to check internet connection.

please download reachability class .h and .m from here

Follow these steps

viewcontroller.h

@class Reachability;

@interface DashboardVC : UIViewController
{

Reachability* internetReachable;
Reachability* hostReachable;

}

-(void) checkNetworkStatus:(NSNotification *)notice;

viewcontroller.m

-(void) viewWillAppear:(BOOL)animated
{
// check for internet connection
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];

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

// now patiently wait for the notification
}


-(void) checkNetworkStatus:(NSNotification *)notice
{
// called after network status changes
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
    case NotReachable:
    {
        NSLog(@"The internet is down.");

        break;
    }
    case ReachableViaWiFi:
    {
        NSLog(@"The internet is working via WIFI.");//your device is connected with wifi


        break;
    }
    case ReachableViaWWAN:
    {
        NSLog(@"The internet is working via WWAN.");//your internet is connected with mobile data


        break;
    }
}

}

Upvotes: 1

Himanshu Patel
Himanshu Patel

Reputation: 1033

You can send a request to your server and given that it's about 5-10 KB of data you expect to be returned, then create a timer callback that is scheduled for say 20 seconds.

If you don't get a response within 20 seconds, then let's consider that a slow connection.

// make POST request to server, the POST request should have a callback method assigned
[self testSpeed];

// schedule a method to be called after 20 seconds
myTimer = [NSTimer scheduledTimerWithInterval:20.0 selector:@selector(stopSpeedTest) .... ];

// your POST request callback method
-(void)speedTestCallback
{
[myTimer invalidate];
myTimer = nil;

   [self alertGoodSpeed];
}

// your stopSpeedTest method to identify app didn't receive response within 20 seconds
-(void)stopSpeedTest
{
   [self alertTooSlow];
}

Upvotes: 0

Himanshu Patel
Himanshu Patel

Reputation: 1033

I think you could try the Reachbility application to help you out checking if you have internet or not. As for the server itself, you can use the NSUrlConnection Delegate methods to check if there was a problem with your request (by seeing the kind of HTTP code that comes)

Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus internetStatus = [reachability currentReachabilityStatus];

if(internetStatus == NotReachable) {
UIAlertView *errorView;

errorView = [[UIAlertView alloc]
             initWithTitle: NSLocalizedString(@"Network error", @"Network error")
             message: NSLocalizedString(@"No internet connection found, this application requires an internet connection to gather the data required.", @"Network error")
             delegate: self
             cancelButtonTitle: NSLocalizedString(@"Close", @"Network error") otherButtonTitles: nil];

[errorView show];
[errorView autorelease];
}

Upvotes: 1

Related Questions