ipad - Check for active internet connection throughout the app in ios using Reachability class -
ipad - Check for active internet connection throughout the app in ios using Reachability class -
i'm building app required sync offline info server whenever net connection active. if net connection lost in between while pushing info server saved in database , whenever connection active force info server. i'm using new reachability class version: 3.5 apple. per illustration particular view controller can this
- (void)viewdidload { [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(reachabilitychanged:) name:kreachabilitychangednotification object:nil]; self.internetreachability = [reachability reachabilityforinternetconnection]; [self.internetreachability startnotifier]; [self updateinterfacewithreachability:self.internetreachability]; } /*! * called reachability whenever status changes. */ - (void) reachabilitychanged:(nsnotification *)note { reachability* curreach = [note object]; nsparameterassert([curreach iskindofclass:[reachability class]]); [self updateinterfacewithreachability:curreach]; } - (void)updateinterfacewithreachability:(reachability *)reachability { if (reachability == self.internetreachability) { //internet active again- phone call api force info server } }
this work particular view controller. there other method in new reachability class check whole app run? or have check in every viewcontroller check active net connection?
you can check appdelegate. have done before.
@property (strong,nonatomic)reachability *reach; @property(nonatomic)networkstatus netstatus; - (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { [[nsnotificationcenter defaultcenter] addobserver:self selector:@selector(checknetworkstatus:) name:kreachabilitychangednotification object:nil]; reach = [reachability reachabilityforinternetconnection]; [reach startnotifier]; [self checknetworkstatus:nil]; } - (void)checknetworkstatus:(nsnotification *)notice { netstatus = [reach currentreachabilitystatus]; if (netstatus == notreachable) { nslog(@"the net down."); // stuff when network gone. } else { nslog(@"the net working!"); // stuff when net comes active [[nsnotificationcenter defaultcenter] postnotificationname:@"internet_available" object:nil]; } }
now when net goes , comes notifies. add together observer notification in view require check net connectivity. working check net throughout app. , properties synthesised.
======== edit
in app delegate.h
+ (bool)isactiveinternet;
and in app delegate.m
+ (bool)isactiveinternet { netstatus = [reach currentreachabilitystatus]; if (netstatus == notreachable) { nslog(@"the net down."); // stuff when network gone. homecoming false; } else { nslog(@"the net working!"); // stuff when net comes active [[nsnotificationcenter defaultcenter] postnotificationname:@"internet_available" object:nil]; homecoming true; } }
so can straight phone call method anywhere in project like
if([appdelegate isactiveinternet]) { //yes net available stuff }
ios ipad ios7 reachability
Comments
Post a Comment