OC-18. Monitor the network status of your iphone

Source: Internet
Author: User
Tags notification center

Use a system approach to monitor network status
  • The method of system is to realize the monitoring of network state through notification mechanism.
  • Steps to implement network status monitoring

    • Define a member variable of type reachability to save the state of the network

      @property (nonatomic, strong) Reachability *reachability;
    • Observers registered as notification hubs

       //registering observers in notification hubs [nsnotificationcenter Defaultcenter] addobserver:self Selector: @selector (getnetworkstatus) Name:kreachabilitychangednotification object: nil]; /*** Getnetworkstatus: The callback method that the Supervisor hears notification * Kreachabilitychangednotification: The type of notification, that is, when the network state changes, Trigger Notification * Object: parameter passed to Getnetworkstatus callback method *///Get and Save network status self< Span class= "hljs-variable" >.reachability = [reachability reachabilityforinternetconnection]; //start listening [self.reachability Startnotifier];              
    • To determine the status of the network in the triggering method of the alert notification

       -(void) Getnetworkstatus{if ([reachability Reachabilityforlocalwifi] .currentreachabilitystatus! = notreachable) {nslog (@ "is WiFi");} else if ([reachability reachabilityforinternetconnection]< Span class= "hljs-variable" >.currentreachabilitystatus! = notreachable) {NSLog (@ "is the mobile phone comes with the network");} else{nslog (@ "network Problematic");}}   
    • Remove the observer from the Notification Center, stop listening, and empty the network listener

      - (void)dealloc{[[NSNotificationCenter defaultCenter] removeObserver:self];[self.reachability stopNotifier];self.reachability = nil;}
Using Afnetworking to monitor network status
  • Monitoring network status via AFN is relatively simple
  • To implement network status monitoring

    • Get an instance of Afnetworkreachabilitymanager

      AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    • Set the block that the manager callback when the network changes

      [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {    NSLog(@"----当前网络状态---%zd", status);}];/*** status:表示当前网络的状态,是个枚举类型变量,具有以下取值*AFNetworkReachabilityStatusUnknown = -1*AFNetworkReachabilityStatusNotReachable = 0*AFNetworkReachabilityStatusReachableViaWWAN = 1*AFNetworkReachabilityStatusReachableViaWiFi = 2*/
    • Start listening.

      [manager startMonitoring];

OC-18. Monitor your iphone's network status

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.