#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
===================================*/
'[ 모바일 관련 ] > 아이폰(iOS)' 카테고리의 다른 글
iOS 문자열 포함 체크 + 문자열 비교 체크 (0) | 2018.11.06 |
---|---|
iOS 소켓통신 참고 (0) | 2018.11.05 |
공통 사용변수 (전역변수, AppDelegate 사용) (0) | 2018.10.29 |
Autosynthesized property '변수' will use synthesized instance variable (0) | 2018.10.29 |
SubView에서 MainView 객체 컨트롤하기 (0) | 2018.10.29 |