失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > ios 网速监控_iOS开发网络篇—监测网络状态

ios 网速监控_iOS开发网络篇—监测网络状态

时间:2022-10-26 07:20:04

相关推荐

ios 网速监控_iOS开发网络篇—监测网络状态

一、说明

在网络应用中,需要对用户设备的网络状态进行实时监控,有两个目的:

(1)让用户了解自己的网络状态,防止一些误会(比如怪应用无能)

(2)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验

WIFI\3G网络:自动下载高清图片

低速网络:只下载缩略图

没有网络:只显示离线的缓存数据

Reachability 类中定义了三种网络状态:

>typedef Num{

NotReachable = 0, //无连接

ReachableViaWiFi, //使用3G/GPRS网络

ReachableViaWWAN //使用WiFi网络

}NetworkStatus;

苹果官方提供了一个叫Reachability的示例程序,便于开发者检测网络状态

二、监测网络状态

Reachability的使用步骤

添加框架SystemConfiguration.framework

添加源代码

示例:

//比如检测某一特定站点的接续状况,可以使用下面的代码

Reachability *reachability = [Reachablity reachabilityWithHostName:@""];

switch([reachabilityStatus]){

caseNotReachable:

break;

case ReachableViaWiFi:

break;

case ReachableViaWWAN:

break;

}

4.检查当前网络环境

//程序启动时,如果想检测可用的网络环境,可以像这样来使用

//是否wifi

+ (BOOL)isEnableWIFI

{

return ([[Reachability reachabiliyForLocalWIFI] currentReachabilityStatus] != NotReachable);

}

//是否3G

+ (BOOL)isEnable3G

{

return ([[Reachability reachabiliyForInternetConnetion] currentReachabilityStatus] != NotReachable);

}

连接状态实时通知

网络连接状态的实时检查,通知在网络应用中也是十分必要的。接续状态发生变化时,需要及时地通知用户。由于Reachability1.5版与2.0版有一些变化,这里分开来说明使用方法。

Reachability 1.5

// My.AppDelegate.h

#import "Reachability.h"

@interface MyAppDelegate : NSObject{

NetworkStatus remoteHostStatus;

}

@property NetworkStatus remoteHostStatus;

@end

// My.AppDelegate.m

#import "MyAppDelegate.h"

@implementation MyAppDelegate

@synthesize remoteHostStatus;

// 更新网络状态

- (void)updateStatus {

self.remoteHostStatus = [[Reachability sharedReachability] remoteHostStatus];

}

// 通知网络状态

- (void)reachabilityChanged:(NSNotification *)note {

[self updateStatus];

if (self.remoteHostStatus == NotReachable) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AppName", nil) message:NSLocalizedString(@"NotReachable", nil)

delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];

[alert show];

[alert release];

}

}

// 程序启动器,启动网络监视

- (void)applicationDidFinishLaunching:(UIApplication *)application {

// 设置网络检测的站点

[[Reachability sharedReachability] setHostName:@""];

[[Reachability sharedReachability] setNetworkStatusNotificationsEnabled:YES];

// 设置网络状态变化时的通知函数

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:)

name:@"kNetworkReachabilityChangedNotification" object:nil];

[self updateStatus];

}

- (void)dealloc {

// 删除通知对象

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

Reachability 2.0

// MyAppDelegate.h

@class Reachability;

@interface MyAppDelegate : NSObject{

Reachability *hostReach;

}

@end

// MyAppDelegate.m

- (void)reachabilityChanged:(NSNotification *)note {

Reachability* curReach = [note object];

NSParameterAssert([curReach isKindOfClass: [Reachability class]]);

NetworkStatus status = [curReach currentReachabilityStatus];

if (status == NotReachable) {

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"AppName""

message:@"NotReachable"

delegate:nil

cancelButtonTitle:@"YES" otherButtonTitles:nil];

[alert show];

}

}

- (void)applicationDidFinishLaunching:(UIApplication *)application {

// ...

// 监测网络情况

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(reachabilityChanged:)

name: kReachabilityChangedNotification

object: nil];

hostReach = [[Reachability reachabilityWithHostName:@""] retain];

[hostReach startNotifer];

}

如果觉得《ios 网速监控_iOS开发网络篇—监测网络状态》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。