Open-source Chinese iOS client learning-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 is confused when writing network detection, I started to think that they are using the ASI class library for network detection. Today, I realized that they are not using the ASI class library for detection, instead, an instance method in the AFNetworking class library is used for network connection detection. First, check the Appdelegate file: In application: didfinishlaunchingwitexceptions: [cpp] // check whether the network exists. If not, the [Config Instance] prompt is displayed. isNetworkRunning = [CheckNetwork isExistenceNetwork]; In applicationDidBecomeActive: [cpp]-(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] ;}} all use [Config Instance]. isNetworkRunning = [CheckNetwork isExistenceNetwork]; then let's look at the Config and CheckNetwork classes to define the Instance method of the Config class. [cpp] 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. isNetworkRunning is the BOOL variable CheckNetwork in the ASIHttp file and put it in a block with the ASI class library. (Chinese characters in the file contain garbled characters) [cpp] + (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;} the Reachability officially provided by Apple was used to check the network connection status, but the scalpers commented out it again. return YES; that is, [Config Instance]. isNetworkRunning = YES; but when I unplug the iMac network cable and run it, I can detect that the network is not connected. I found that 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 is successfully executed in the success block. The block does not return values. Two parameters are accepted to create the request and respond to the data request. If the URL request fails, the method in failure is executed, this block still does not return values. It accepts two parameters to create a request and the NSError object to describe the network or resolution error. Therefore, the network connection detection in the above section is available, take one of the source code as an example.

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.