Reachability* reachability = [Reachability reachabilityForInternetConnection]; [reachability startNotifIEr]; [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangednotification object:nil];
而我的可达性更改选择器方法如下
- (voID)reachabilityChanged:(NSNotification*)notification{ Reachability* reachability = notification.object; if(reachability.currentReachabilityStatus == NotReachable) NSLog(@"Internet off"); else NSLog(@"Internet on");}
但是当我关闭我的飞行模式,以及当我在手机中获得网络连接时,我没有收到任何通知.
我错过了什么吗?
解决方法 我使用appdelegate中的变量将当前网络状态存储为bool@property (nonatomic,assign) BOol hasInet;
.M
- (BOol)application:(UIApplication *)application dIDFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ [self setUpRechability];}-(voID)setUpRechability{ [[NSNotificationCenter defaultCenter] addobserver:self selector:@selector(handleNetworkChange:) name:kReachabilityChangednotification object:nil]; reachability = [Reachability reachabilityForInternetConnection]; [reachability startNotifIEr]; NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus]; if (remoteHostStatus == NotReachable) {NSLog(@"no"); self.hasInet-=NO; } else if (remoteHostStatus == ReachableViaWiFi) {NSLog(@"wifi"); self.hasInet-=YES; } else if (remoteHostStatus == ReachableViaWWAN) {NSLog(@"cell"); self.hasInet-=YES; }}- (voID) handleNetworkChange:(NSNotification *)notice{ NetworkStatus remoteHostStatus = [reachability currentReachabilityStatus]; if (remoteHostStatus == NotReachable) {NSLog(@"no"); self.hasInet-=NO; } else if (remoteHostStatus == ReachableViaWiFi) {NSLog(@"wifi"); self.hasInet-=YES; } else if (remoteHostStatus == ReachableViaWWAN) {NSLog(@"cell"); self.hasInet-=YES; }// if (self.hasInet) {// UIAlertVIEw *alert=[[UIAlertVIEw alloc]initWithTitle:@"Net avail" message:@"" delegate:self cancelbuttonTitle:OK_EN otherbuttonTitles:nil,nil];// [alert show];// }}总结
以上是内存溢出为你收集整理的如何从iOS可达性类更改网络连接通知?全部内容,希望文章能够帮你解决如何从iOS可达性类更改网络连接通知?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)