Use the Reachability class to determine the current network connection type for IOS devices _ios

Source: Internet
Author: User


(1). Download Https://developer.apple.com/library/ios/samplecode/Reachability/Reachability.zip

(2). Drag reachability.h,reachability.m into the project (Library non arc)

Arc:-fno-objc-arc

(3). Import Systemconfiguration.framework

(4). Usage

Copy Code code as follows:

-(nsstring*) Getnettype

{

nsstring* result;

reachability *r = [reachability reachabilitywithhostname:@ "www.baidu.com"];
NSLog (@ "====:%i", [R Currentreachabilitystatus]);
switch ([R Currentreachabilitystatus]) {

Case notreachable://No network connection


result=@ "No network connection";

Break

Case reachableviawwan://using 3G network

result=@ "3g";

Break

Case reachableviawifi://using WiFi network

result=@ "WiFi";

Break

}
NSLog (@ "casereachableviawwan=%i", Reachableviawwan);
NSLog (@ "casereachableviawifi=%i", Reachableviawifi);
return result;

}

(5) If the difference between 2G, 2.5G, 3G network cannot be distinguished, then we can refactor the Networkstatusforflags method in REACHABILITY.M:

Copy Code code as follows:

-(NetworkStatus) Networkstatusforflags: (scnetworkreachabilityflags) Flags
{
if ((Flags & kscnetworkreachabilityflagsreachable) = = 0)
{
return notreachable;
}

BOOL retVal = notreachable;

if ((Flags & kscnetworkreachabilityflagsconnectionrequired) = = 0)
{
If target host is reachable and no connection are required
Then we ' ll assume (for now) that your on Wi-Fi
RetVal = Reachableviawifi;
}


if (((Flags & Kscnetworkreachabilityflagsconnectionondemand)!= 0) | |
(Flags & Kscnetworkreachabilityflagsconnectionontraffic)!= 0))
{
... and the connection is On-demand (or on-traffic) if the
Calling application is using the Cfsocketstream or higher APIs
if ((Flags & kscnetworkreachabilityflagsinterventionrequired) = = 0)
{
... and no [user] intervention is needed
RetVal = Reachableviawifi;
}
}

if ((Flags & kscnetworkreachabilityflagsiswwan) = = Kscnetworkreachabilityflagsiswwan)
{
if ((Flags & kscnetworkreachabilityflagsreachable) = = kscnetworkreachabilityflagsreachable) {
if ((Flags & kscnetworkreachabilityflagstransientconnection) = = kscnetworkreachabilityflagstransientconnection) {
RetVal = REACHABLEVIA3G;
if ((Flags & kscnetworkreachabilityflagsconnectionrequired) = = kscnetworkreachabilityflagsconnectionrequired) {
RetVal = reachablevia2g;
}
}
}
}
return retVal;
}

Check if the current network connection is normal
-(BOOL) connectedtonetwork
{
struct sockaddr_in zeroaddress;
Bzero (&zeroaddress, sizeof (zeroaddress));
Zeroaddress.sin_len = sizeof (zeroaddress);
zeroaddress.sin_family = af_inet;

Scnetworkreachabilityref defaultroutereachability = scnetworkreachabilitycreatewithaddress (NULL, struct sockaddr *) &zeroaddress);
Scnetworkreachabilityflags flags;

BOOL didretrieveflags = Scnetworkreachabilitygetflags (defaultroutereachability, &flags);
Cfrelease (defaultroutereachability);

if (!didretrieveflags) {
printf ("Error.") Count not recover network reachability flags\n ");
return NO;
}

BOOL isreachable = flags & kscnetworkflagsreachable;
BOOL needsconnection = flags & kscnetworkflagsconnectionrequired;
Return (isreachable &&!needsconnection)? Yes:no;
}

Check network connection type
-(void) Checknetworktype: (ID) sender{
NSString *connectionkind;
if ([self connectedtonetwork]) {
Hostreach = [reachability reachabilitywithhostname:@ "www.google.com"];
Switch ([Hostreach currentreachabilitystatus]) {
Case Notreachable:
Connectionkind = @ "no network link";
Break
Case Reachableviawifi:
Connectionkind = @ "The network type currently in use is WiFi";
Break
Case REACHABLEVIA3G:
Connectionkind = @ "The type of network link currently in use is Wwan (3G)";
Break
Case REACHABLEVIA2G:
Connectionkind = @ "The type of network link currently in use is Wwan (2G)";
Break
Default
Break
}
}else {
Connectionkind = @ "no network link";
}
}

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.