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