IOS---Detect network status

Source: Internet
Author: User

 
The network state of the device for reachability 1.5.typedef enum {    notreachable = 0,  //no connection    Reachableviacar Rierdatanetwork,//Use 3G/GPRS network    reachableviawifinetwork  //Use WiFi network} networkstatus;//the Device for reachability 2.0.typedef enum {    notreachable = 0,  //No connection    Reachableviawifi,  //using 3G/GPRS Network    Reachableviawwan  //Use WiFi network} networkstatus;

For example, to detect the continuation status of a particular site, you can use the following code:

reachability *r =  [reachabilityforlocalwifi];
switch ([R Currentreachabilitystatus]) {case    notreachable:        //No network connection break        ;    Case Reachableviawwan:        //Use 3G network break        ;    Case Reachableviawifi:        //Use WiFi network break        ;}

Add Reachability.h and REACHABILITY.M to your project and use Systemconfiguration.framework to make it work.

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{

if ([reachability reachabilityforinternetconnection].currentreachabilitystatus==notreachable) | |  ([reachability reachabilityforlocalwifi].currentreachabilitystatus==notreachable)]  {

Uialertview *alert=[[uialertview alloc]initwithtitle:@"Warning" message:@ "your device is temporarily free of network!  "Delegate:self cancelbuttontitle:@"determines "Otherbuttontitles:nil";

[Alert show];

}

}

Add real-time monitoring of network changes

Referencing in APPDELETEGATE.M

#import "Reachability.h"
@interface Appdelegate ()
@property (Nonatomic,strong) reachability *conn;
@end

@implementation Appdelegate

-(void) reachabilitychanged: (nsnotification *) Note
{
reachability* Curreach = [Note Object];
Nsparameterassert ([Curreach iskindofclass: [reachability class]]);
NetworkStatus status = [Curreach currentreachabilitystatus];
Switch (status)
{
Case Notreachable:
No network connection
NSLog (@ "Network is not Connected");
Break
Case Reachableviawwan:
Using the 2G/3G Network
NSLog (@ "Connected cellular data");
Break
Case Reachableviawifi:
Use WiFi network
NSLog (@ "Connected WiFi");
Break
}
}

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions {
Add monitoring
[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (reachabilitychanged:) Name: Kreachabilitychangednotification Object:nil];
Self.conn = [reachability reachabilityforinternetconnection];
[Self.conn Startnotifier];
return YES;
}

IOS---Detect 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.