objective c - How to get change in network connection notification from iOS Reachability Class? -


hi want capture whenever user gets network connectivity in application have added apples reachability class , below snippet using in appdelegate class didfinishlaunchingwithoptions method,

reachability* reachability = [reachability reachabilityforinternetconnection];         [reachability startnotifier];         [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(reachabilitychanged:) name:kreachabilitychangednotification object:nil]; 

and reachabilitychanged selector method below

- (void)reachabilitychanged:(nsnotification*)notification {     reachability* reachability = notification.object;     if(reachability.currentreachabilitystatus == notreachable)         nslog(@"internet off");     else         nslog(@"internet on"); } 

but here not getting kind of notification when switch off airplane mode , when network connectivity in phone.

am missing anything?

i use variable in appdelegate store current network status 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]; //    } } 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -