iOS 获取网络状态
生活随笔
收集整理的這篇文章主要介紹了
iOS 获取网络状态
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在iOS開發者,獲取網絡狀態比較常用
?
-(NSString *)getNetWorkStates{UIApplication *app = [UIApplication sharedApplication];NSArray *children = [[[app valueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];NSString *state = nil;int netType = 0;//獲取到網絡返回碼for (id child in children) {if ([child isKindOfClass:NSClassFromString(@"UIStatusBarDataNetworkItemView")]) {//獲取到狀態欄netType = [[child valueForKeyPath:@"dataNetworkType"]intValue];switch (netType) {case 0:state = @"無網絡";//無網模式break;case 1:state = @"2G";break;case 2:state = @"3G";break;case 3:state = @"4G";break;case 5:{state = @"WIFI";}break;default:break;}}}//根據狀態選擇return state; }?
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的iOS 获取网络状态的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery操作json
- 下一篇: 删除git commit 的 UserI