Just a Computer Graphics Studio & My Life

寫iOS的APP時,幾乎任何應用都需要確認網路狀態,畢竟iPhone和iPad都是行動裝置,在這充滿無線網路的環境之下,我們會希望使用者的網路狀態良好,因而發揮我們精心設計的APP功能。

在Xcode中的流程如下:

  1. 加入框架:SystemConfiguration.framework
  2. 引入下兩個標頭檔:
    #import <netinet/in.h>
    #import <SystemConfiguration/SystemConfiguration.h>
  3. 示範函式如下:

-(void)getNetworkStatus
{
    //Create zero addy
    struct sockaddr_in Addr;
    bzero(&Addr, sizeof(Addr));
    Addr.sin_len = sizeof(Addr);
    Addr.sin_family = AF_INET;

    //結果存至旗標中
    SCNetworkReachabilityRef target = SCNetworkReachabilityCreateWithAddress(NULL, (struct sockaddr *) &Addr);
    SCNetworkReachabilityFlags flags;
    SCNetworkReachabilityGetFlags(target, &flags);

    //將取得結果與狀態旗標位元做AND的運算並輸出
    (flags & kSCNetworkFlagsReachable)
    ?NSLog(@"kSCNetworkFlagsReachable:YES")
    :NSLog(@"kSCNetworkFlagsReachable:NO");
    (flags & kSCNetworkReachabilityFlagsIsWWAN)
    ?NSLog(@"kSCNetworkReachabilityFlagsIsWWAN:YES")
    :NSLog(@"kSCNetworkReachabilityFlagsIsWWAN:NO");
    (flags & kSCNetworkReachabilityFlagsReachable)
    ?NSLog(@"kSCNetworkReachabilityFlagsReachable:YES")
    :NSLog(@"kSCNetworkReachabilityFlagsReachable:NO");
    (flags & kSCNetworkReachabilityFlagsTransientConnection)
    ?NSLog(@"kSCNetworkReachabilityFlagsTransientConnection:YES")
    :NSLog(@"kSCNetworkReachabilityFlagsTransientConnection:NO");
    (flags & kSCNetworkReachabilityFlagsConnectionRequired)
    ?NSLog(@"kSCNetworkReachabilityFlagsConnectionRequired:YES")
    :NSLog(@"kSCNetworkReachabilityFlagsConnectionRequired:NO");
    (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic)
    ?NSLog(@"kSCNetworkReachabilityFlagsConnectionOnTraffic:YES")
    :NSLog(@"kSCNetworkReachabilityFlagsConnectionOnTraffic:NO");
    (flags & kSCNetworkReachabilityFlagsInterventionRequired)
    ?NSLog(@"kSCNetworkReachabilityFlagsInterventionRequired:YES")
    :NSLog(@"kSCNetworkReachabilityFlagsInterventionRequired:NO");
    (flags & kSCNetworkReachabilityFlagsConnectionOnDemand)
    ?NSLog(@"kSCNetworkReachabilityFlagsConnectionOnDemand:YES")
    :NSLog(@"kSCNetworkReachabilityFlagsConnectionOnDemand:NO");
    (flags & kSCNetworkReachabilityFlagsIsLocalAddress)
    ?NSLog(@"kSCNetworkReachabilityFlagsIsLocalAddress:YES")
    :NSLog(@"kSCNetworkReachabilityFlagsIsLocalAddress:NO");
    (flags & kSCNetworkReachabilityFlagsIsDirect)
    ?NSLog(@"kSCNetworkReachabilityFlagsIsDirect:YES")
    :NSLog(@"kSCNetworkReachabilityFlagsIsDirect:NO");
}

執行結果:(開啟區域WiFi)

2013-01-03 13:49:42.535 HouseBook[4881:907] kSCNetworkFlagsReachable:YES

2013-01-03 13:49:42.537 HouseBook[4881:907] kSCNetworkReachabilityFlagsIsWWAN:NO

2013-01-03 13:49:42.537 HouseBook[4881:907] kSCNetworkReachabilityFlagsReachable:YES

2013-01-03 13:49:42.538 HouseBook[4881:907] kSCNetworkReachabilityFlagsTransientConnection:NO

2013-01-03 13:49:42.539 HouseBook[4881:907] kSCNetworkReachabilityFlagsConnectionRequired:NO

2013-01-03 13:49:42.540 HouseBook[4881:907] kSCNetworkReachabilityFlagsConnectionOnTraffic:NO

2013-01-03 13:49:42.541 HouseBook[4881:907] kSCNetworkReachabilityFlagsInterventionRequired:NO

2013-01-03 13:49:42.542 HouseBook[4881:907] kSCNetworkReachabilityFlagsConnectionOnDemand:NO

2013-01-03 13:49:42.543 HouseBook[4881:907] kSCNetworkReachabilityFlagsIsLocalAddress:YES

2013-01-03 13:49:42.544 HouseBook[4881:907] kSCNetworkReachabilityFlagsIsDirect:NO

執行結果:(關閉區域WiFi)

2013-01-03 14:04:47.798 HouseBook[1176:707] kSCNetworkFlagsReachable:NO

2013-01-03 14:04:47.800 HouseBook[1176:707] kSCNetworkReachabilityFlagsIsWWAN:NO

2013-01-03 14:04:47.801 HouseBook[1176:707] kSCNetworkReachabilityFlagsReachable:NO

2013-01-03 14:04:47.802 HouseBook[1176:707] kSCNetworkReachabilityFlagsTransientConnection:NO

2013-01-03 14:04:47.803 HouseBook[1176:707] kSCNetworkReachabilityFlagsConnectionRequired:NO

2013-01-03 14:04:47.804 HouseBook[1176:707] kSCNetworkReachabilityFlagsConnectionOnTraffic:NO

2013-01-03 14:04:47.806 HouseBook[1176:707] kSCNetworkReachabilityFlagsInterventionRequired:NO

2013-01-03 14:04:47.808 HouseBook[1176:707] kSCNetworkReachabilityFlagsConnectionOnDemand:NO

2013-01-03 14:04:47.809 HouseBook[1176:707] kSCNetworkReachabilityFlagsIsLocalAddress:NO

2013-01-03 14:04:47.811 HouseBook[1176:707] kSCNetworkReachabilityFlagsIsDirect:NO

我們最想知道台灣最常使用的無線網路3G和WiFi如何判斷:

  • kSCNetworkFlagsReachable參數來判斷WiFi的狀態
  • kSCNetworkReachabilityFlagsIsWWAN參數來判斷3G的狀態

SDK裡的定義:

@constant kSCNetworkFlagsReachable

This flag indicates that the specified nodename or address can be reached using the current network configuration.

@constant kSCNetworkReachabilityFlagsIsWWAN

This flag indicates that the specified nodename or address can be reached via an EDGE, GPRS, or other “cell" connection.

總結就是,蘋果把檢查網路狀態做得相當鉅細靡遺,畢竟世界有太多不同的通訊協定,而我們台灣目前最主流的無線網路只有廣為人知的3G和WiFi,於是只好自己來簡化它。

參考:SCNetworkReachability Reference利用 SCNetworkReachability 取得網路服務狀態的方法SCNetworkReachability API(或-怎樣檢查 iPhone 的網路狀態)How to check for an active Internet Connection on iPhone SDK?

廣告

發表留言

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s

標籤雲

%d 位部落客按了讚: