#import "AppDelegate.h"
#import <AFNetworking/AFNetworkReachabilityManager.h>
@interface AppDelegate ()
@property(nonatomic,strong)NSString *netStr;
@property(nonatomic,strong)AFNetworkReachabilityManager *manager;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//网络监听
[self netWorkMonitoringAction];
}
-(void)netWorkMonitoringAction{
//创建网络监听管理者对象
_manager = [AFNetworkReachabilityManager sharedManager];
__weak typeof (self) weakSelf =self;
//设置监听网络状态
[_manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusUnknown:
weakSelf.netStr = @"未识别的网络";
break;
case AFNetworkReachabilityStatusNotReachable:{
weakSelf.netStr = @"未连接网络";
}
break;
case AFNetworkReachabilityStatusReachableViaWiFi:{
weakSelf.netStr = @"已连接wifi";
}
break;
case AFNetworkReachabilityStatusReachableViaWWAN:{
weakSelf.netStr = @"正在使用2G,3G,4G网络";
}
break;
default:
break;
}
[weakSelf drawNetWorkLayer:weakSelf.netStr];
}];
//开始监听
[_manager startMonitoring];
}
-(void)dealloc{
//停止监听网络状况.
[_manager stopMonitoring];
}
- (void)drawNetWorkLayer:(NSString *)string{
CGRect rect = [string boundingRectWithSize:CGSizeMake(ScreenWidth-16, 2000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
UILabel *netWorkLayer = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/2-(rect.size.width+50)/2, ScreenHeigth/2-50, rect.size.width+50, 30)];
netWorkLayer.text = string;
netWorkLayer.textColor = [UIColor whiteColor];
netWorkLayer.layer.cornerRadius = 5;
netWorkLayer.layer.masksToBounds = YES;
netWorkLayer.backgroundColor = [UIColor blackColor];
netWorkLayer.textAlignment = NSTextAlignmentCenter;
[self.window addSubview:netWorkLayer];
[UIView animateWithDuration:5.0 animations:^{
netWorkLayer.alpha = 0.0;
}];
}
使用AFNetWorking 监听网络
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 一般我们在开发iOS项目的过程中,会使用第三方库Reachability来监听网络状态,区分网络类型(2G,3G,...