我正在开发一个iot应用程序,如果wifi/celluar网络速度较慢,我想显示一个弹出窗口。 如何在目标c中实现慢速网络警报?如何检查目标c中的celluar/wifi网络实力?
回答
我想你可以试试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];
}
这无法确定网络状态为慢还是快! –
@TejaNandamuri检查第二个答案。 –
不要复制粘贴答案。而是您可以投票将其关闭为重复! –
您可以发送一个请求到服务器,然后给出它大约需要返回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];
}
查看互联网连接的完整示例。
请从这里
下载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;
}
}
}
- 1. WinRT中的网络检查(C#实现)
- 2. 如何检查从网络
- 3. 检查C#中的安全网络#
- 4. 如何检查C中的网络设备状态?
- 5. 查找重力的目标c
- 6. 检查目标C中的互联网连接C
- 7. 目标C和Java之间的网络
- 8. 在Android中检查网络
- 9. 获得无线网络实力
- 10. 如何检查网络访问SBJson URL
- 11. 如何检查网络连接?
- 12. 如何检查网络更改iphone
- 13. 如何检查网络可达性
- 14. 如何检查网络元素当前不可见,硒,C#
- 15. 如何用Qt 5.2快速检查网络路径(C++)
- 16. 如何检查目标c中的JSON格式?
- 17. Swift中的IPv6网络上的网络连接检查崩溃
- 18. 如何检查网页标题中硒
- 19. 如何查找gprs实力?
- 20. android中的网络检查功能?
- 21. 检查Flash中的网络连接?
- 22. 检查libGDX中的网络连接
- 23. 检查片段中的网络连接
- 24. 如何检测网络中的变化?
- 25. 集中我的目标C应用程序的网络代码
- 26. Python - 检查网络图
- 27. 检查网络连接
- 28. 检查网络连接iPhone
- 29. 检查网络可在android
- 30. 如何检查Windows Azure中的网络总流量(虚拟机)
检查以下链接可能对你有用的可达性http://stackoverflow.com/a/7938778/5184217并为您缓慢的互联网http://stackoverflow.com/a/13910178/5184217 –