iOS开发之 app网络实时监测
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS开发之 app网络实时监测相关的知识,希望对你有一定的参考价值。
1.在
#import "AppDelegate.h"
{
Reachability *hostReach;
}
2.在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {}里
//监测网络情况
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(reachabilityChanged:)
name: kReachabilityChangedNotification
object: nil];
//初始化Reachability类,并添加一个监测的网址。
hostReach = [Reachability reachabilityWithHostName:@"https://www.baidu.com"];
//开始监测
[hostReach startNotifier];
3.
#pragma mark - 监测网络情况,当网络发生改变时会调用
- (void)reachabilityChanged:(NSNotification *)note {
Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);
NetworkStatus status = [curReach currentReachabilityStatus];
if (status == NotReachable) {
//初始化提示框;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否清理缓存" preferredStyle: UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//点击按钮的响应事件;
}]];
//弹出提示框;
[self.window.rootViewController presentViewController:alert animated:true completion:nil];
}
}
以上是关于iOS开发之 app网络实时监测的主要内容,如果未能解决你的问题,请参考以下文章