iOS development detects if there is an Internet connection

Source: Internet
Author: User

To request a network using AF:

Add the class method inside the AppDelegate.h:

+ (void) networkstatus;//Get network status

Inside the. m

#import   "AppDelegate.h" #import   "ViewController.h" #import   "AFNetworkReachabilityManager.h"// The class #import  "FileManager.h"//simple interest model for monitoring network status in AF is used to record the current network status @interface appdelegate  () @ end@implementation appdelegate-  (BOOL) Application: (uiapplication *) application  Didfinishlaunchingwithoptions: (nsdictionary *) Launchoptions {    viewcontroller  *view = [[viewcontroller alloc] init];    uinavigationcontroller  *nav = [[UINavigationController alloc] initWithRootViewController:view];     self.window.rootviewcontroller = nav;        // Call Network status     [AppDelegate netWorkStatus];         return yes;} +  (void) networkstatus{    /**      afnetworkreachabilitystatusunknown          = -1,  //  Unknown       afnetworkreachabilitystatusnotreachable     = 0,   //  No connection      afnetworkreachabilitystatusreachableviawwan = 1,   //  3G  spending      AFNetworkReachabilityStatusReachableViaWiFi = 2,    // wifi     */        //   If you want to detect changes in network status, you must use the startmonitoring    [[afnetworkreachabilitymanager  of the Test manager's Singleton sharedmanager] startmonitoring];        //  a single case for detecting network connections, Callback methods for network changes     [[AFNetworkReachabilityManager sharedManager]  setreachabilitystatuschangeblock:^ (Afnetworkreachabilitystatus status)  {                  //this is the simple interest + model, used to record network status         filemanager *maa =  [filemanager sharefilemanager];        maa.netstate =  status;        //         nslog (@ "-----network status----%ld---%d",  status,maa.netstate);     }];}

Create a. h file named Singleton

  Singleton.h//  SingletonModel////  Created by  Lin Xiao Xi  on  15/5/21.//  copyright  (c)  2015   Lin Xiao XI.  all rights reserved.////because of the method name  shared "ClassName" is connected, in order for the macro to correctly replace the signature "ClassName" need to precede the  ##//when the macro definition more than one line, at the end with "\" to indicate the next line is also within the scope of the macro definition. Note that the last line does not need to add "\". How to use: (Simple interest mate model is very convenient to use)//In. h (//public access to simple interest object method Singleton_interface (MyModel))//Inside. M (Singleton_implementation ( MyModel)) #define  SINGLETON_INTERFACE (Class)   +  (class *) share# #class; #define   Singleton_implemetntion (Class) static class * instance = nil;+  (class *) share# #class {if  (!instance)  {instance = [[class alloc]init];} Return instance;} +  (class *) Allocwithzone: (struct _nszone *) zone{static dispatch_once_t onece; Dispatch_once (&onece, ^{instance = [super allocwithzone:zone];}); Return instance;}

Then create the model, named FileManager

Inside the FileManager.h.

#import <Foundation/Foundation.h> #import "Singleton.h" @interface filemanager:nsobjectsingleton_interface ( FileManager) @property (nonatomic,assign) int netstate; @end

Inside the FILEMANAGER.M.

#import "FileManager.h" @implementation filemanagersingleton_implemetntion (FileManager) @end


If the ASI is used:

Import the header file inside the AppDelegate.h:

#import "Reachability.h"

To add a class method:

+ (BOOL) isconnectionavailable; //Determine if there is a network

Inside the. m

+ (BOOL)  isConnectionAvailable{        BOOL  Isexistencenetwork = yes;        reachability *reach  = [reachability reachabilitywithhostname:@ "Www.apple.com"];         nsparameterassert ([Reach iskindofclass:[reachability class]]);         NetworkStatus stats = [reach currentReachabilityStatus];     if  (stats == notreachable)          //No internet         isExistenceNetwork = NO;     else if  (STATS&NBSP;==&NBSP;REACHABLEVIA3G)          isExistenceNetwork = YES;    else if  (stats ==  REACHABLEVIA2G)         isexistencenetwork = yes;    else if  (stats ==  Reachableviawifi)         isExistenceNetwork = YES;         if  (!isexistencenetwork)  {                 return no;    }         return isexistencenetwork;}

You can also use the simple interest + model to record the specific state of the current network in the way above!

iOS development detects if there is an Internet connection

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.