OC, oc Language

Source: Internet
Author: User
Tags notification center

OC, oc Language
Use the system method to monitor the network status

  • The system monitors the network status through the notification mechanism.
  • Steps for network status monitoring

    • Define Reachability member variables to save the network status

      @property (nonatomic, strong) Reachability *reachability;
    • Observer registered as the notification center

      // Register the observer [[NSNotificationCenter defacenter center] addObserver: self selector: @ selector (getNetworkStatus) name: kReachabilityChangedNotification object: nil] in the notification center;/*** getNetworkStatus: callback Method * kReachabilityChangedNotification: Notification type, that is, when the network status changes, the notification is triggered * object: the parameter passed to the getNetworkStatus callback Method * // gets and saves the network status self. reachability = [Reachability reachabilityForInternetConnection]; // start listening to [self. reachability startNotifier];
    • Determine the network status in the trigger Method for listening to notifications

      -(Void) getNetworkStatus {if ([Reachability reachabilityForLocalWiFi]. currentReachabilityStatus! = NotReachable) {NSLog (@ "WIFI");} else if ([Reachability reachabilityForInternetConnection]. currentReachabilityStatus! = NotReachable) {NSLog (@ "bring your phone network");} else {NSLog (@ "network problems ");}}
    • Remove the observer from the notification center, stop the listener, and clear the network listener object.

      - (void)dealloc{[[NSNotificationCenter defaultCenter] removeObserver:self];[self.reachability stopNotifier];self.reachability = nil;}
Use AFNetworking to monitor network status
  • It is relatively simple to monitor the network status through AFN
  • Network Status Monitoring

    • Obtain the AFNetworkReachabilityManager instance

      AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    • Sets the callback Block for the manager when the network changes.

      [Manager setReachabilityStatusChangeBlock: ^ (AFNetworkReachabilityStatus status) {NSLog (@ "---- current network status --- % zd", status) ;}];/*** status: indicates the status of the current network. It is an enumeration type variable and has the following values: * AFNetworkReachabilityStatusUnknown =-1 * AFNetworkReachabilityStatusNotReachable = 0 * Running = 1 * Running = 2 */
    • Start listening

      [manager startMonitoring];

       

Related Article

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.