- 最简便的集成方法当属pod: pod ‘RealReachability’。
- 手动集成:将RealReachability文件夹加入到工程即可。
- 依赖:Xcode5.0+,支持ARC, iOS6+.项目需要引入SystemConfiguration.framework.
//打开检测
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
1.[GLobalRealReachability startNotifier];
return YES;
}
//在具体的页面添加观察者(实时监测网络的变化)
- (void)viewDidLoad
{
[super viewDidLoad];
2.[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(networkChanged:)
name:kRealReachabilityChangedNotification
object:nil];
//当前网络连接状态 (单例调用简单)
3.ReachabilityStatus status = [GLobalRealReachability currentReachabilityStatus];
NSLog(@"Initial reachability status:%@",@(status));
if (status == RealStatusNotReachable)
{
self.flagLabel.text = @"Network unreachable!";
}
if (status == RealStatusViaWiFi)
{
self.flagLabel.text = @"Network wifi! Free!";
}
if (status == RealStatusViaWWAN)
{
self.flagLabel.text = @"Network WWAN! In charge!";
}
}
//观察者实时检测方法
- (void)networkChanged:(NSNotification *)notification
{
RealReachability *reachability = (RealReachability *)notification.object;
ReachabilityStatus status = [reachability currentReachabilityStatus];
ReachabilityStatus previousStatus = [reachability previousReachabilityStatus];
NSLog(@"networkChanged, currentStatus:%@, previousStatus:%@", @(status), @(previousStatus));
if (status == RealStatusNotReachable)
{
self.flagLabel.text = @"Network unreachable!";
}
if (status == RealStatusViaWiFi)
{
self.flagLabel.text = @"Network wifi! Free!";
}
if (status == RealStatusViaWWAN)
{
self.flagLabel.text = @"Network WWAN! In charge!";
}
WWANAccessType accessType = [GLobalRealReachability currentWWANtype];
if (status == RealStatusViaWWAN)
{
if (accessType == WWANType2G)
{
self.flagLabel.text = @"RealReachabilityStatus2G";
}
else if (accessType == WWANType3G)
{
self.flagLabel.text = @"RealReachabilityStatus3G";
}
else if (accessType == WWANType4G)
{
self.flagLabel.text = @"RealReachabilityStatus4G";
}
else
{
self.flagLabel.text = @"Unknown RealReachability WWAN Status, might be iOS6";
}
}
}
iOS下的实际网络连接检测:RealReachability
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 网络连接状态检测对于我们的iOS app开发来说是一个非常通用的需求。为了更好的用户体验,我们会在无网络时展现本地...