아이폰어플개발정보2010. 6. 24. 11:40
아이폰 어플개발에서 인터넷 접속여부를 체크하는 방법

by alex 13. 2월 2009 10:41

#import

#include

- (BOOL) connectedToNetwork

{

    // Create zero addy

    struct sockaddr_in zeroAddress;

    bzero(&zeroAddress, sizeof(zeroAddress));

    zeroAddress.sin_len = sizeof(zeroAddress);

    zeroAddress.sin_family = AF_INET;

 
    // Recover reachability flags

    SCNetworkReachabilityRef defaultRouteReachability = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *)&zeroAddress);

    SCNetworkReachabilityFlags flags;

 
    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);

    CFRelease(defaultRouteReachability);

 
    if (!didRetrieveFlags)

    {

        printf("Error. Could not recover network reachability flags\n");

        return 0;

    }

 
    BOOL isReachable = flags & kSCNetworkFlagsReachable;

    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;

 BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;

    return ((isReachable && !needsConnection) || nonWiFi) ?

 (([[NSURLConnection alloc] initWithRequest:[NSURLRequest

     requestWithURL: [NSURL URLWithString:@"http://www.apple.com/"]

     cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:20.0]

           delegate:self]) ? YES : NO) : NO;

}



출처 : http://blog.naver.com/PostView.nhn?blogId=amoros21&logNo=140107111390
Posted by 오늘마감

댓글을 달아 주세요