IOS network development network status check

Source: Internet
Author: User

Classes used in network connections:

1. reachability

1. Add the. h and. m Files of Reachability, and then add systemconfiguration. framework.

2. Three Network statuses are defined in reachability:

Typedef num {

Notreachable = 0, // No connection

Reachableviawifi, // use 3g/GPRS network

Reachableviawwan // use WiFi

} Networkstatus;

3. Example:

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

Switch ([reachabilitystatus]) {

Case notreachable:

// Todo

Break;

Case reachableviawifi:

// Todo

Break;

Case reachableviawwan:

// Todo

Break;

}

4. Check the current network environment

When the program starts, if you want to detect the available network environment, you can use it like this

// Whether WiFi is enabled

+ (Bool) isenablewifi

{

Return ([[reachability reachabiliyforlocalwifi] currentreachabilitystatus]! = Notreachable );

 
}

 

// Whether it is 3 GB

+ (Bool) isenable3g

{

Return ([[reachability reachabiliyforinternetconnetion] currentreachabilitystatus]! = Notreachable );

}

 

Example:

-(Void) viewwillappear :( bool) animated

{

If ([reachability reachabiliyforinternetconnetion]. currentreachabilitystatus = notreachable) & [reachability reachabiliyforlocalwifi]. currentreachabilitystatus = notreachable ))

{

Self. navigationitem. hidesbackbutton = yes;

[Self. navigationitem setleftbarbuttonitem: Nil animated: No];

}

 

}

 

5. Real-time notification of Link Status

Real-time check. When the continuous status changes, users must be notified in a timely manner:

 

Reachability 1.5
// Myappdelegate. h

# Import "reachability"

@ Interface myappdelegate: nsobject <uiapplicationdelegate>
{

}

@ Property networkstatus remotehoststatus;

@ End

 

// Myappdelegate. m

# Import "myappdelegate. H"

@ Implementation myappdelegate
@ Synthesize remotehoststatus;

// Update the network status
-(Void) updatestatus
{
Self. remotehoststatus = [[reachability sharedreachability] remotehoststatus];
}

// Notifies the network status
-(Void) reachabilitychanged :( nsnotification *) Note
{
[Self updatestatus];
If (self. remotehoststatus = notreachable)
{
Uialert * Alert = [[uialertview alloc] initwithtitle: nslocalizedstring (@ "appname", nil)
Message: nslocalizedstring (@ "notreachable", nil );
Delegate: Nil cancelbuttontitle: @ "OK"
Otherbuttontitles: Nil];

[Alert show];
[Alert release];
}
}

// Program initiator to start Network Monitoring
-(Void) applicationdidfinishlaunching :( uiapplication *) Application
{
// Set the site for Network Monitoring
[[Reachability sharedreachability] sethostname: @ "www.baidu.com"];
[[Reachability sharedreachability] setnetworkstatusicationsenabled: Yes];

// Set the notification function when the network status changes
[[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (reachabilitychanged :)
Name: @ "knetworkreachabilitychangednotification" Object: Nil];
[Self updatestatus];

}

-(Void) dealloc
{
// Delete the notification object
[[Nsicationcenter center defacenter center] removeobserver: Self];
[Window release];
[Super dealloc];
}

 

Reachability 2.0
// Myappdelegate. h

# Import "reachability"
@ Class reachability;
@ Interface myappdelegate: nsobject <uiapplicationdelegate>
{
Reachability * hostreach;
}

@ End
 
// Myappdelegate. m

# Import "myappdelegate. H"

@ Implementation myappdelegate

// Notifies the network status
-(Void) reachabilitychanged :( nsnotification *) Note
{
Reachability * currentreach = [Note object];
Nsparameterassert ([currentreach iskindofclass: [reachability class]);
Networkstatus status = [currentreach currentreachabilitystatus];

If (status = notreachable)
{
Uialert * Alert = [[uialertview alloc] initwithtitle: nslocalizedstring (@ "appname", nil)
Message: nslocalizedstring (@ "notreachable", nil );
Delegate: Nil cancelbuttontitle: @ "yes"
Otherbuttontitles: Nil];

[Alert show];
[Alert release];
}
}

// Program initiator to start Network Monitoring
-(Void) applicationdidfinishlaunching :( uiapplication *) Application
{
//....

// Monitor network conditions
[[Nsicationcenter center defacenter center] addobserver: Self selector: @ selector (reachabilitychanged :)
Name: @ "knetworkreachabilitychangednotification" Object: Nil];
Hostreach = [[reachability reachabilitywithhostname: @ "www.baidu.com"] retain];
// Hostreach startcycler];
//...

}

 

2. Other common classes.

1. nsurl

2. nsurlrequest

3. nsmutableurlrequest is a subclass of nsurlrequest. You can set some request parameters.

4. nsurlresponse

5. nserror

 

 

 

 

 

 

 

 

 

 

 

 

 

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.