iOS监听 网络信号变化

来源:互联网 发布:淘宝卖家店铺名称 编辑:程序博客网 时间:2024/05/15 07:10

</pre>步骤一: 在appdelegate中加入 notification 监听信号变化<p></p><p></p><p class="p1"></p><pre name="code" class="objc">- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStatusIsChange:) name:kReachabilityChangedNotification object:nil];   }-(void)networkStatusIsChange:(NSNotification*)notify{    Reachability* reachability=_reachability;        NSString* netStatus;        switch (reachability.currentReachabilityStatus) {        case ReachableViaWiFi:            NSLog(@"这是在Wifi下");            netStatus=@"WIFI";            break;        case ReachableViaWWAN:            NSLog(@"这是在移动网络下");            netStatus=@"WWAN";            break;        case NotReachable:            NSLog(@"无网络");            netStatus=@"NONE";            break;                    default:            break;    }    NSUserDefaults* ud=[NSUserDefaults standardUserDefaults];    [ud setObject:netStatus forKey:UD_NET_STATUS];    [ud synchronize];        [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFY_NET_STATUS_CHANGE object:netStatus];}



步骤二: 在需要监听的controllerview 中加入notification的监测者 即可

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNetStatusIsChange:) name:NOTIFY_NET_STATUS_CHANGE object:nil];



0 0
原创粉丝点击