4
4

2 回答 2

3
于 2012-12-25T02:41:09.443 回答
0

在 didFinishLaunchingWithOptions 方法上放:

self.internetReach= [Reachability reachabilityWithHostName:@"your host name e.g.www.apple.com"];

[internetReach startNotifier];


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil];


[self performSelector:@selector(reachabilityChanged:) withObject:[NSNotification notificationWithName:kReachabilityChangedNotification object:internetReach]];

这将调用以下方法

- (void)reachabilityChanged:(NSNotification*)note

{

Reachability* curReach = [note object];
NSParameterAssert([curReach isKindOfClass: [Reachability class]]);

if(curReach == self.internetReach)
{
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
    switch (netStatus)
    {
        case ReachableViaWiFi:
        {            
            isInternetConnectionAvilable=YES;
            if(isNetworkNotifierCalledOnce)
            {
                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Message!" message:@"Internet Available Now" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];
                [alert release];
            }
            break;
        }

        case NotReachable:
        {     

            isInternetConnectionAvilable=NO;

            UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Message!" message:@"No Internet Connectivity" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

            [alert show];

            [alert release];

            break;
        }
    }

}

isNetworkNotifierCalledOnce=YES;

}

于 2012-12-25T07:24:23.093 回答