iOS Network monitoring method

Source: Internet
Author: User

Method One (official):

Reachability

================================================================================

Frame Address: Https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip

How to use

1> Adding a networked status listener object

@property (nonatomic, strong) reachability *reach;

2> Instantiating listener objects

Self.reach = [reachability reachabilitywithhostname:@ "www.baidu.com"];

3> Determining the status of networking

-(void) netwrokstatuschanged {

Switch (self.reach.currentReachabilityStatus) {

Case Notreachable:

Sjlog (@ "No Internet connection");

Break

Case Reachableviawifi:

Sjlog (@ "internet via Wi-Fi");

Break

Case Reachableviawwan:

Sjlog (@ "via 3G Internet");

Break

Default

Break

}

}

4> real-time monitoring of network status using notification hubs

[[Nsnotificationcenter Defaultcenter] Addobserver:selfselector: @selector (netwrokstatuschanged) Name: Kreachabilitychangednotification Object:nil];

[Self.reach Startnotifier];

-(void) Dealloc {

Registers the specified notification listener

[[Nsnotificationcenter Defaultcenter] removeObserver:selfname:kReachabilityChangedNotification Object:nil];

}

method Two (AFN):

Managers who have access to network monitoring

Afnetworkreachabilitymanager *mgr = [Afnetworkreachabilitymanager Sharedmanager];

Set up processing after a change in network status

[Mgr setreachabilitystatuschangeblock:^ (afnetworkreachabilitystatus status) {

Switch (status) {

Case Afnetworkreachabilitystatusunknown://Unknown network

Sjlog (@ "Unknown network");

Break

Case afnetworkreachabilitystatusnotreachable://No network (off-grid)

Sjlog (@ "No network detected");

Break

Case Afnetworkreachabilitystatusreachableviawwan://Phone comes with network

Sjlog (@ "via 3G Internet");

Break

Case Afnetworkreachabilitystatusreachableviawifi://WIFI

Sjlog (@ "internet via Wi-Fi");

Break

}

}];

Start monitoring

[Mgr Startmonitoring];

iOS Network monitoring method

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.