Network environment judgment for iOS

Source: Internet
Author: User

There are two ways to judge the network environment
1, reachability
2, afnetworking in Afnetworkreachabilitymanager

Reachability steps
(1) Import reachability class name
(2) Initialize the Reachability object, HostName try to use a more stable network
(3) Adding observers, receiving notification of changes in the network environment
(4) Start monitoring
(5) The object to be reachability in the notice

Code instance
1 Importing header files

Import "Reachability.h"

2 creating Reachability objects try to use a more stable website

     Reachability *reachability = [Reachability reachabilityWithHostName:@"www.baidu.com"];

3 Add an observer to receive notification of changes in the network environment

/
Kreachabilitychangednotification the name of the notification that the network environment has changed
NetworkStatus enumeration of network environments
-(NSString
) string currentreachabilitystring the network environment
*/

  [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(change:) name:kReachabilityChangedNotification object:nil];

4 Start monitoring
-(BOOL) Startnotifier; Start notification monitoring
-(void) stopnotifier; End of monitoring

[reachability startNotifier];

5 getting notified of Reachability objects getting network status
Notfication.object; Get Reachability Object

  • (void) Change: (nsnotification ) notfication{
    //Notfication.object; Get reachability object
    //5 Get notified that the Reachability object gets the network status
    reachability
    reachability = notfication.object;
    NSLog (@ "%@", [reachability currentreachabilitystring]);
    NSString *status = @ "";
    Switch (reachability.currentreachabilitystatus) {

    case NotReachable:{    status = @"无网络";} break;case ReachableViaWiFi:{ status = @"WIFI网络";} break;case ReachableViaWWAN:{ status = @"WAN网络";} break;default: break;

    }
    NSLog (@ "%@", status);
    }

Here's the second method
Afnetworking
(1) Import class Library
(2) Create a Detection object class method Sharemanager
(3) Start monitoring
(4) Get the test result callback method by detecting object (Setreachabilitystatuschangeblock:)

Code instance
1) Import the class library

Import "AFNetworking.h"

2) Initialize the Afnetworkreachabilitymanager object

 AFNetworkReachabilityManager *reachabilityMannger =   [AFNetworkReachabilityManager sharedManager];

3) Start monitoring
-(void) startmonitoring;
-(void) stopmonitoring;

[reachabilityMannger startMonitoring];

4) access to monitored network status

[Reachabilitymannger setreachabilitystatuschangeblock:^ (afnetworkreachabilitystatus status) {NSString *result =@ "";            switch (status) {case Afnetworkreachabilitystatusunknown: result = @ "Unknown Network"; break; case afnetworkreachabilitystatusnotreachable:result = @ "No Network" ; break; case afnetworkreachabilitystatusreachableviawwan:result = @ " WAN "; break; case afnetworkreachabilitystatusreachableviawifi:result = @ " WIFI "; break; default: break;} nslog (@ "%@", result);}];       

PS: Both of these methods need to download download reachability or afnetworking
Taking searchafnetworking as an example
Method: Open Terminal pod searchafnetworking
Afnetworking (3.0.4)
A delightful IOS and OS X networking framework.
Pod ' afnetworking ', ' ~> 3.0.4 '

    • Homepage:https://github.com/afnetworking/afnetworking
    • Source:https://github.com/afnetworking/afnetworking.git
      Find the resource path to download



The Poison of Love in Love (Jane book author)
Original link: http://www.jianshu.com/p/4fbce026418a
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

Network environment judgment for iOS

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.