首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Objective c中检查c/wifi网络强度?

如何在Objective c中检查c/wifi网络强度?
EN

Stack Overflow用户
提问于 2017-04-03 21:17:35
回答 3查看 730关注 0票数 0

我正在开发一个物联网应用程序,我想显示一个弹出窗口,以防wifi/celluar互联网速度慢。如何在objective c中实现慢速网络告警?

EN

回答 3

Stack Overflow用户

发布于 2017-04-03 21:20:58

我想你可以试试Reachbility应用程序来帮你检查你有没有上网。至于服务器本身,您可以使用NSUrlConnection Delegate方法来检查您的请求是否存在问题(通过查看附带的HTTP代码)

代码语言:javascript
运行
复制
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
EN

Stack Overflow用户

发布于 2017-04-04 00:02:58

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

请从此处下载

按照以下步骤操作

viewcontroller.h

代码语言:javascript
运行
复制
@class Reachability;

@interface DashboardVC : UIViewController
{

Reachability* internetReachable;
Reachability* hostReachable;

}

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

viewcontroller.m

代码语言:javascript
运行
复制
-(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;
    }
}

}
票数 1
EN

Stack Overflow用户

发布于 2017-04-03 22:11:02

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

如果您在20秒内没有收到响应,那么我们就认为这是一个慢速连接。

代码语言:javascript
运行
复制
// 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];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43185707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档