2017-04-03 109 views
-1

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

+1

检查以下链接可能对你有用的可达性http://stackoverflow.com/a/7938778/5184217并为您缓慢的互联网http://stackoverflow.com/a/13910178/5184217 –

回答

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

这无法确定网络状态为慢还是快! –

+0

@TejaNandamuri检查第二个答案。 –

+0

不要复制粘贴答案。而是您可以投票将其关闭为重复! –

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

}