[Objective-c Tip] ios 네트워크 체크하기


네트워크 통신을 하기 위해서는 네트워크 환경 체크는 필수 입니다.

* 기타 강력한 네트워크 프레임웍을 사용하시려면 git에서 AFNetworking을 추천해드립니다

https://github.com/AFNetworking/AFNetworking

-----------------------------------------------------------------------------

Objective-c에서 제공하는 프레임 워크로, 네트워크 변동사항을 체크할수 있습니다.

#import <SystemConfiguration/SystemConfiguration.h>
#import <netinet/in.h>
- (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) {
          UIAlertView *alert = [[UIAlertView alloc]
                                     initWithTitle:@"Wifi 네크워크에 연결되었습니다."
                                     message:nil
                                     delegate:self 
                                     cancelButtonTitle:nil
                                     otherButtonTitles:@"확인",nil];
          [alert show];
          [alert release];
         
     }
     else if(isReachable && !needsConnection && nonWiFi){
          UIAlertView *alert = [[UIAlertView alloc]
                                     initWithTitle:@"3G 네트워크 연결"
                                     message:@"3G 네트워크 이용시 데이터 이용료가 부과됩니다.\nWi-Fi로 접속하시면 더욱 원활하게\n서비스를 이용하실 수 있습니다."
                                     delegate:self 
                                     cancelButtonTitle:nil
                                     otherButtonTitles:@"확인",nil];
          [alert show];
          [alert release];
         
     }
     else {
          UIAlertView *alert = [[UIAlertView alloc]
                                     initWithTitle:@"연결 없음"
                                     message:@"네트워크 연결이 필요합니다.\n사용 가능한 Wifi 네트워크나\n3G 네트워크에 접속해 주세요."
                                     delegate:self 
                                     cancelButtonTitle:nil
                                     otherButtonTitles:@"확인",nil];
          [alert show];
          [alert release];
         
     }
}

댓글

주간 인기글

카드뉴스 마케팅 팁

[ubuntu] 신규 계정에 sudo 권한 추가하기

[Vue] 전화번호 입력/조회시 '-' 자동으로 넣어주기

[Objective-C] NSString(문자열) 다루기

안드로이드에서 당겨서 새로고침(SwipeRefreshLayout) 쉽게 구현하기