IOS reachability determine if the requested server timed out?

Source: Internet
Author: User

The method of acquiring/detecting network state is described in Apple's routine reachability.

1. Use reachability in your program to copy only the Reachability.h and reachability.m in the routine to your project.

2. Then add the systemconfiguration.framework into the project.

The version I am using is: version:2.2

I added a global to Apple's routines--Reachabilityautochecker

. h

@interface Reachabilityautochecker:nsobject@property (nonatomic, retain) reachability  *reachability; @property ( Nonatomic, assign) NetworkStatus networkstatus; @property (nonatomic, assign) BOOL          connectionrequired; @end

. m file

@implementation reachabilityautochecker@synthesize reachability;+ (ID) sharedchecker{    static Reachabilityautochecker *staticchecker = nil;    if (!staticchecker) {        staticchecker = [[Reachabilityautochecker alloc] init];        [[Nsnotificationcenter Defaultcenter] Addobserver:staticchecker selector: @selector (reachabilitychanged:) Name: Kreachabilitychangednotification Object:nil];        Staticchecker.networkstatus = notreachable;        staticchecker.connectionrequired = NO;    }    return staticchecker;} -(void) reachabilitychanged: (nsnotification*) note{    reachability* Curreach = [Note Object];    Nsparameterassert ([Curreach iskindofclass: [reachability class]]);    Self.networkstatus = [Curreach currentreachabilitystatus];    self.connectionrequired = [Curreach connectionrequired];} @end

I added a category for Apple's routines-reachability (Autochecker)

. h file:

@interface reachability (autochecker) + (void) startcheckwithreachability: (reachability *) reachability;+ (BOOL) isreachable; @end

. m File:

@implementation reachability (autochecker) + (void) startcheckwithreachability: (reachability *) reachability{        Reachabilityautochecker *checker = [Reachabilityautochecker sharedchecker];        if (checker.reachability) {[checker.reachability stopnotifier];    Checker.reachability = nil;    } checker.reachability = reachability; [Checker.reachability startnotifier];}        + (BOOL) isreachable{reachabilityautochecker *checker = [Reachabilityautochecker sharedchecker];        if (!checker.reachability) {NSLog (@ "Check reachability with No reachability have been set!");    return NO;        } networkstatus networkstatus = [Checker networkstatus];    if (NetworkStatus = = Reachableviawifi) {NSLog (@ "WIFI");    } if (networkstatus = = Reachableviawwan) {NSLog (@ "3G");    } BOOL connectionrequired = NO;    connectionrequired = [Checker connectionrequired]; #if kshouldprintreachabilityflags NSLog (@ "NetworkStatus%d ConnectionrEquired%d ", NetworkStatus, connectionrequired); #endif if (networkstatus) return YES; else return NO;} @end

Call Mode:

Appdelegate.m

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOptions{    To detect the continuation status of a particular site, you can use the following code    reachability *preachability = [reachability reachabilitywithhostname:@] Appservices.comcsoft.com "];    Start monitoring network Status    [reachability startcheckwithreachability:preachability];        Self.window = [[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]];    Override point for customization after application launch.    Self.viewcontroller = [[Viewcontroller alloc] initwithnibname:@ "Viewcontroller" bundle:nil];    Self.window.rootViewController = Self.viewcontroller;    [Self.window makekeyandvisible];    return YES;}

Viewcontroller.m

-(Ibaction) Upinside_checknetstatus: (ID) sender{    if (![ reachability isreachable])    {        Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Prompt"                                                        message:@ "Sorry , network exception, please check your network settings. "                                                       delegate:self                                              cancelbuttontitle:@" good "                                              otherbuttontitles:nil";        [Alert show];        return;}    }

Runtime Reminders: #error "This class needs to be compiled in a non-ARC environment, add the-FNO-OBJC-ARC tag"

Network normal NSLog as follows:

2013-07-05 14:15:53.084 prj_reachability[8153:11303] reachability Flag Status:-R-------Networkstatusforflags

2013-07-05 14:15:54.265 prj_reachability[8153:11303] WIFI

2013-07-05 14:15:54.266 prj_reachability[8153:11303] NetworkStatus 1 connectionrequired 0

IOS reachability determine if the requested server timed out? Go

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.