如何在Objective c中检查celluar / wifi互联网强度?

时间:2017-04-03 13:17:35

标签: objective-c uiview iot internet-connection alerts

我正在开发一个iot应用程序,我想显示一个弹出窗口,以防wifi / celluar上网速度慢。 如何在目标c中实现慢速网络警报?

3 个答案:

答案 0 :(得分:1)

我认为您可以尝试使用Reachbility应用程序来帮助您检查是否有互联网。至于服务器本身,您可以使用NSUrlConnection Delegate方法检查您的请求是否存在问题(通过查看出现的HTTP代码类型)

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

答案 1 :(得分:1)

检查互联网连接的完整示例。

请从此处下载reachability class .h and .m

  

请按照以下步骤操作

<强> 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;
    }
}

}

答案 2 :(得分:0)

您可以向服务器发送请求,并且假设您希望返回大约5-10 KB的数据,然后创建一个计划为20秒的计时器回调。

如果您在20秒内未收到回复,请考虑缓慢连接。

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