Network tips for iOS (e\2g\3g\4g\wifi\ no net)

Source: Internet
Author: User

1. Introduction

The Web tip is an indispensable module in app development. Now the application needs intelligence, the request according to the network changes can automatically make the reasonable processing (including prompt, download ...) , etc.). The processing of the network is also very simple, the implementation of a few lines of code (this is because Apple has organized the network changes). Apple officially provides a monitoring network to change the file name to reachability. for reachability also has its shortcomings, it can not be more detailed to determine the user is the kind of mobile phone network, just provide developers a general designation of the expression.

2. Official introduction of Reachability

reachability (official) method is to open Xcode, colleagues hold "COMMAND + SHIFT +/" can be recalled this interface, as follows:


The following image appears after clicking Documentation and API Reference:


In the red (red is what I marked, in fact there is no) in the box input reachability, then will pop up:


Click Reachability in the Red box , and then pop up the following diagram:


When you click Open Project, open and download the project.


3, the lack of official reachability

Look at the official given monitoring return type as follows:


From where we can see the official, supportive situation. Lack of mobile phone network mode integration, can not distinguish between 2g\3g\4g, E and other networks.

4. We extend the official reachability to support the 2~4G and E Networks.

1. Modify the official return network enumeration, such as:



2 "Add #import <CoreTelephony/CTTelephonyNetworkInfo.h> libraries, such as:


3 "Core code change area, such as:

#pragma mark-network Flag handling-(networkstatus) Networkstatusforflags: (scnetworkreachabilityflags) flags{ Printreachabilityflags (Flags, "networkstatusforflags"); if ((Flags & kscnetworkreachabilityflagsreachable) = = 0) {    The target host is not Reachable.return notreachable;}         NetworkStatus returnvalue = notreachable;if ((Flags & kscnetworkreachabilityflagsconnectionrequired) = = 0) {/*         If the target host is reachable and no connection are required then we'll assume (for now) so you ' re on Wi-Fi ... */returnvalue = 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 &am P kscnetworkreachabilityflagsinterventionrequired) = = 0) {/* ... and no [user] intervention is needed ... */returnvalue = Reachableviawifi; }}if (Flags & Kscnetworkreachabilityflagsiswwan) = = Kscnetworkreachabilityflagsiswwan) {/* ... but WWAN Co         Nnections is OK if the calling application is using the Cfnetwork APIs. */        /*!        Version compatible */float systemversion = [Uidevice currentdevice].systemversion.floatvalue;            if (systemversion>=7.0) {cttelephonynetworkinfo * telephonynetwork = [Cttelephonynetworkinfo new]; /*!            Gets the name of the current network */nsstring * networknamestring = [telephonynetwork currentradioaccesstechnology]; if (networknamestring) {/*! Network differentiated ctradioaccesstechnologygprs-2g CT RADIOACCESSTECHNOLOGYEDGE-2G (sometimes called 2.5G) ctradioaccesstechnologywcdma-3g C TRADIOACCESSTECHNOLOGYHSDPA-3G (sometimes called 3.5G) Ctradioaccesstechnologyhsupa-3g ctradioaccesstechnologycdma1x-2g ctradioaccesstechnologycdmaevdorev0-3g                 CTRADIOACCESSTECHNOLOGYCDMAEVDOREVA-3G ctradioaccesstechnologycdmaevdorevb-3g CTRADIOACCESSTECHNOLOGYEHRPD-3G (or 3.5-EHRPD is-to-allow migration from CDMA EVDO to LTE) CT  RADIOACCESSTECHNOLOGYLTE-4G (allowed to being called 4G by the ITU as mentioend above) */IF ([Networknamestring isequaltostring:ctradioaccesstechnologygprs]| | [Networknamestring isequaltostring:ctradioaccesstechnologyedge]| | [Networknamestring isequaltostring:ctradioaccesstechnologycdma1x])                {returnvalue = reachablevia2g; }else if ([Networknamestring isequaltostring:ctradioaccesstechnologywcdma]| | [Networknamestring isequaltostring:ctradioaccesstechnologyhsdpa]| | [Networknamestring isequaltostring:ctradioaccesstechnologyhsupa]| | [Networknamestring Isequaltostring:ctradioaccesstechnologyhsupa]| | [Networknamestring isequaltostring:ctradioaccesstechnologycdmaevdorev0]| | [Networknamestring isequaltostring:ctradioaccesstechnologycdmaevdoreva]| | [Networknamestring isequaltostring:ctradioaccesstechnologycdmaevdorevb]| | [Networknamestring ISEQUALTOSTRING:CTRADIOACCESSTECHNOLOGYEHRPD])                {returnvalue = reachablevia3g; }else if ([networknamestring Isequaltostring:ctradioaccesstechnologylte]) {returnvalue = ReachableVia4G                ;                }else{returnvalue = Reachableviawwan;            }}else{returnvalue = Reachableviawwan;        }}else{/*! Low version */returnvalue = Reachableviawwan; }}return returnvalue;}

5, do not set up the network processing, do compatible, such as:


6, The complete demo

Link: https://pan.baidu.com/s/1hsysOPe Password: jz3q

7.


Network tips for iOS (e\2g\3g\4g\wifi\ no net)

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.