开发者

iphone:How to check each & every time internet connection in iphone?

Hello friends, I want to che开发者_如何学JAVAck internet connection each & every time is active or not in my iPhone apps so please tell me any link or any idea to develop this functionality.

Thanks in advance..


Use Reachability Classes

+ (BOOL)isNetworkAvailable {

    Reachability *internetReach;
    internetReach = [Reachability reachabilityForInternetConnection];
    [internetReach startNotifier];
    NetworkStatus netStatus = [internetReach currentReachabilityStatus];
    if(netStatus == NotReachable) { 
        NSLog(@"Network Unavailable");
        return NO;
    }
    else
        return YES;
}


//check for internet connection
NSStringEncoding enc;
NSError *error;
NSString *connected = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.apple.com"] usedEncoding:&enc error:&error];
    if (connected == nil) {
        //no internet connection..display alert
    } else {
       //call your function
    }

This should help you.


You can use the Reachability example from apple developer.

I use it like this:

-(BOOL) isInternetReachable 
{
    Reachability *r = [Reachability reachabilityForInternetConnection];
    NetworkStatus internetStatus = [r currentReachabilityStatus];
    if(internetStatus == NotReachable) 
    {
        return NO;
    }
    return YES;
}

You can also check to see if a certain server is up with the provided methods.


With the help of Reachability Class and also go through...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜