Open-source Chinese IOS client learning-(6) network connection Detection

Source: Internet
Author: User

The open-source Chinese IOS client synchronizes data over the network to achieve the same data effect as the web page, so you need to check the network when starting the program. This is also the first step to start every online application, if the online applications do not check the network, Apple will not pass the review;

The open-source Chinese IOS client experts were confused when writing network detection. They started to think that they were using the network detection implemented by the ASI class library. Today, they only understood that, they do not use the ASI class library detection, but use an instance method in the afnetworking class library for network connection detection;

First look at the appdelegate file:

In application: didfinishlaunchingwitexceptions: method

// Check whether the network exists. If not, the prompt [config instance]. isnetworkrunning = [checknetwork isexistencenetwork] is displayed.

In applicationdidbecomeactive: Method

-(Void) applicationdidbecomeactive :( uiapplication *) Application {[config instance]. isnetworkrunning = [checknetwork isexistencenetwork]; If ([config instance]. isnetworkrunning = No) {uialertview * myalert = [[uialertview alloc] initwithtitle: @ "warning" message: @ "No network connection, the offline mode" delegate: Self cancelbuttontitle: @ "Confirm" otherbuttontitles: nil, nil]; [myalert show] ;}}

[Config instance]. isnetworkrunning = [checknetwork isexistencenetwork] is used. Let's look at the definition of config and checknetwork.

The instance method of the config class,

static Config * instance = nil;+(Config *) Instance{    @synchronized(self)    {        if(nil == instance)        {            [self new];        }    }    return instance;}

It can also be inferred from the literal meaning that it is used to instantiate the cinfig class, And isnetworkrunning is a bool variable.

Checknetwork is placed in the asihttp file together with the ASI class library. (garbled characters appear in the file)

+ (Bool) isexistencenetwork {// bool isexistencenetwork; // reachability * r = [reachability failed: @ "www.oschina.net"]; // switch ([R currentreachabilitystatus]) {// case notreachable: // isexistencenetwork = false; // nslog (@ "℃ "); // break; // case reachableviawwan: // isexistencenetwork = true; /// nslog (@ "your e-pilot instances cross-region 3G secure connections"); // break; // case reachableviawifi: // isexistencenetwork = true; // nslog (@ "{e} WiFi "); // break; // return isexistencenetwork; return yes ;}

Originally, the reachability provided by Apple was used to check the network connection status, but the scalpers commented out and returned yes;

That is, [config instance]. isnetworkrunning = yes;

However, when I unplug the iMac network cable and run it, I can detect that the network is not connected. By searching for "no connection to the wrong network"


They are all in the afnetworking class library.

-(Void) postpath :( nsstring *) path parameters :( nsdictionary *) parameters success :( void
(^) (Afhttprequestoperation * operation, Id responseobject) Success Failure :( void (^) (afhttprequestoperation * operation, nserror * error) in the failure method,

This method uses afhttprequestoperation and "patch" to request the HTTP client operation queue, and uses block blocks (IOS 4.0 + features). The URL request successfully performs operations in the success block, here, the block does not return values. It accepts two parameters, creates a request and responds to data requests. If the URL request fails to execute the method in failure, the block still does not return values, accepts two request creation parameters and the nserror object to describe the network or resolution error;

So we have the network connection detection above. Let's take one of the source code as an example.




The learning process is in progress. Please correct the mistakes. Please contact us and learn together;

Welcome to share, please indicate the source of http://blog.csdn.net/duxinfeng2010

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.