본문으로 바로가기

#import <SystemConfiguration/SystemConfiguration.h>

#import <netinet/in.h>





/*===================================

 네트워크 상태 체크 START

 ===================================*/

- (void) connectedToNetwork

{

    

    struct sockaddr_in zeroAddress;

    bzero(&zeroAddress, sizeof(zeroAddress));

    zeroAddress.sin_len = sizeof(zeroAddress);

    zeroAddress.sin_family = AF_INET;

    

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

    SCNetworkReachabilityFlags flags;

    

    BOOL didRetrieveFlags = SCNetworkReachabilityGetFlags(defaultRouteReachability, &flags);

    CFRelease(defaultRouteReachability);

    

    if (!didRetrieveFlags)

    {

        NSLog(@"error");

    }

    

    BOOL isReachable = flags & kSCNetworkFlagsReachable;

    BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;

    BOOL nonWiFi = flags & kSCNetworkReachabilityFlagsTransientConnection;

    

    

    if (isReachable && !needsConnection && !nonWiFi) {

        //Wifi 연결

        NSLog(@"Wifi 연결");

    }else if(isReachable && !needsConnection && nonWiFi){

        //3G 연결

        NSLog(@"3G 연결");

    }else {

        

        UIAlertController * alert = [UIAlertController

                                     alertControllerWithTitle:@"네트워크 오류"

                                     message:@"네트워크 연결이 필요합니다.\n사용 가능한 Wifi 네트워크나\n3G 네트워크에 접속해 주세요."

                                     preferredStyle:UIAlertControllerStyleAlert];

        

        UIAlertAction* yesButton = [UIAlertAction

                                    actionWithTitle:@"확인"

                                    style:UIAlertActionStyleDefault

                                    handler:^(UIAlertAction * action) {

                                        //Handle your yes please button action here

                                    }];

        [alert addAction:yesButton];

        

        /*

        UIAlertAction* noButton = [UIAlertAction

                                   actionWithTitle:@"No, thanks"

                                   style:UIAlertActionStyleDefault

                                   handler:^(UIAlertAction * action) {

                                       //Handle no, thanks button

                                   }];

        [alert addAction:noButton];

         */

        

        [self presentViewController:alert animated:YES completion:nil];

        

        

        //네트워크 오류 이미지 뷰 노출

        

        //새로고침 이미지 버튼 노출

        

        

    }

}

/*===================================

 네트워크 상태 체크 END

 ===================================*/