iOS判斷是否存在網路
Every iPhone developer that has integrated a network connection based application has had to follow the Apple HID (Human Interface Design) rules. This means, that in order to get the Apple reviewers to sign-off on your application, you have to pass the network connectivity test. Basically, in the case of a catastrophic network disconnect issue with 3G and WiFi does your application properly alert the user that it will not perform properly without the usage of the network? I’ve seen a lot of workaround techniques where users attempt to do an HTTP GET on say Google or some other website. Clearly, this kind of technique will work sporadically in an unknown environment common to the mobile device. I mean, imagine causing your application users to sit through a 5-30 second web request timeout. The end-user will probably believe that the application is hung. The ideal solution happens to be handed to us in the API Framework SCNetworkReachability. (Make sure your codebase is linked properly with the “SystemConfiguration” Framework).
It’s worked for me and I recommend it for doing a simple network connectivity test.
- #import <sys/socket.h>
- #import <netinet/in.h>
- #import <arpa/inet.h>
- #import <netdb.h>
- #import <SystemConfiguration/SCNetworkReachability.h>
- //Snip, you know we're in the implementation...
#pragma mark -是否存在網路
- (BOOL)isNetworkReachable{
// 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)
{
return NO;
}
BOOL isReachable = flags & kSCNetworkFlagsReachable;
BOOL needsConnection = flags & kSCNetworkFlagsConnectionRequired;
return (isReachable && !needsConnection) ? YES : NO;
}
相關文章
- sh指令碼判斷路徑是否存在指令碼
- 41:判斷元素是否存在
- shell 判斷檔案或路徑是否存在
- 判斷網路是否連線
- JavaScript 判斷函式是否存在JavaScript函式
- postgresql如何判斷表是否存在SQL
- golang判斷檔案是否存在Golang
- vc判斷檔案是否存在
- jQuery如何判斷元素是否存在jQuery
- 如何判斷Javascript物件是否存在JavaScript物件
- MySQL判斷表名是否存在MySql
- shell判斷系統路徑中是否存在空格
- Android 判斷網路是否正常Android
- 判斷objectStore物件倉庫是否存在Object物件
- js判斷dom節點是否存在JS
- 怎麼判斷mysql表是否存在MySql
- jQuery 判斷使用者是否存在jQuery
- Laravel 5 判斷條件是否存在Laravel
- python 判斷檔案是否存在Python
- jQuery 判斷頁面元素是否存在jQuery
- iOS判斷網路狀態iOS
- java判斷mysql中資料庫是否存在JavaMySql資料庫
- jQuery如何判斷一個元素是否存在jQuery
- 如何判斷一個jquery物件是否存在jQuery物件
- 如何利用jQuery判斷指定元素是否存在jQuery
- javascript判斷一個變數是否存在JavaScript變數
- js如何判斷一個物件是否存在JS物件
- JavaScript中判斷是否存在某屬性JavaScript
- QTP中如何判斷Excel程式是否存在?QTExcel
- 判斷是否能連線網際網路
- Python科研武器庫 - 檔案/路徑操作 - 判斷路徑是否存在Python
- mysql如何判斷是否存在某個欄位MySql
- jquery怎麼樣判斷檔案是否存在jQuery
- Linux判斷URL是否存在,並返回IPLinux
- 如何判斷頁面是否存在某個元素
- js如何判斷指定的檔案是否存在JS
- js如何判斷一個函式是否存在JS函式
- jquery判斷元素是否存在於陣列中jQuery陣列