最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS實(shí)現(xiàn)實(shí)時(shí)檢測(cè)網(wǎng)絡(luò)狀態(tài)的示例代碼

 更新時(shí)間:2017年07月14日 09:20:58   作者:_Jack_Yang  
網(wǎng)絡(luò)連接狀態(tài)檢測(cè)對(duì)于我們的iOS開(kāi)發(fā)來(lái)說(shuō)是一個(gè)非常通用的需求。下面這篇文章主要就給大家介紹了關(guān)于利用iOS實(shí)現(xiàn)實(shí)時(shí)檢測(cè)網(wǎng)絡(luò)狀態(tài)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。

前言

在網(wǎng)絡(luò)應(yīng)用中,需要對(duì)用戶設(shè)備的網(wǎng)絡(luò)狀態(tài)進(jìn)行實(shí)時(shí)監(jiān)控,有兩個(gè)目的:

(1)讓用戶了解自己的網(wǎng)絡(luò)狀態(tài),防止一些誤會(huì)(比如怪應(yīng)用無(wú)能)

(2)根據(jù)用戶的網(wǎng)絡(luò)狀態(tài)進(jìn)行智能處理,節(jié)省用戶流量,提高用戶體驗(yàn)

  WIFI\3G網(wǎng)絡(luò):自動(dòng)下載高清圖片

  低速網(wǎng)絡(luò):只下載縮略圖

  沒(méi)有網(wǎng)絡(luò):只顯示離線的緩存數(shù)據(jù)

最近在工作中遇到一個(gè)功能就是根據(jù)用戶當(dāng)前的網(wǎng)絡(luò)狀,用戶未聯(lián)網(wǎng)需要提示一下,如果是Wifi可以推薦一些圖片新聞,如果是3G模式設(shè)置為無(wú)圖的模式,獲取網(wǎng)絡(luò)狀態(tài)比較簡(jiǎn)單,畢竟中國(guó)現(xiàn)在的流量還是一個(gè)比較貴的狀態(tài),哪天用戶發(fā)現(xiàn)App消耗流量過(guò)多說(shuō)不定就干掉了App 。 不過(guò)蘋果的 Reachability 都解決了以上問(wèn)題,使用起來(lái)也比較方便,所以就總結(jié)以下,具體的稍微簡(jiǎn)單分析下,下面話不多說(shuō),來(lái)一起看看詳細(xì)的介紹:

示例代碼

Reachability.h頭文件代碼:

#import <Foundation/Foundation.h> 
#import <SystemConfiguration/SystemConfiguration.h> 
#import <netinet/in.h> 
 
//http://www.cnblogs.com/xiaofeixiang 
typedef enum : NSInteger { 
 NotReachable = 0, 
 ReachableViaWiFi, 
 ReachableViaWWAN 
} NetworkStatus; 
 
 
extern NSString *kReachabilityChangedNotification; 
 
 
@interface Reachability : NSObject 
 
/*! 
 * Use to check the reachability of a given host name. 
 */ 
+ (instancetype)reachabilityWithHostName:(NSString *)hostName; 
 
/*! 
 * Use to check the reachability of a given IP address. 
 */ 
+ (instancetype)reachabilityWithAddress:(const struct sockaddr_in *)hostAddress; 
 
/*! 
 * Checks whether the default route is available. Should be used by applications that do not connect to a particular host. 
 */ 
+ (instancetype)reachabilityForInternetConnection; 
 
/*! 
 * Checks whether a local WiFi connection is available. 
 */ 
+ (instancetype)reachabilityForLocalWiFi; 
 
/*! 
 * Start listening for reachability notifications on the current run loop. 
 */ 
- (BOOL)startNotifier; 
- (void)stopNotifier; 
 
- (NetworkStatus)currentReachabilityStatus; 
 
/*! 
 * WWAN may be available, but not active until a connection has been established. WiFi may require a connection for VPN on Demand. 
 */ 
- (BOOL)connectionRequired; 
 
@end

Reachability.m文件:

#import <arpa/inet.h> 
#import <ifaddrs.h> 
#import <netdb.h> 
#import <sys/socket.h> 
#import <CoreFoundation/CoreFoundation.h> 
#import "Reachability.h" 
NSString *kReachabilityChangedNotification = @"kNetworkReachabilityChangedNotification"; 
#pragma mark - Supporting functions 
#define kShouldPrintReachabilityFlags 1 
static void PrintReachabilityFlags(SCNetworkReachabilityFlags flags, const char* comment) 
{ 
#if kShouldPrintReachabilityFlags 
 NSLog(@"Reachability Flag Status: %c%c %c%c%c%c%c%c%c %s\n", 
  //當(dāng)前網(wǎng)絡(luò)2G/3G/4G蜂窩網(wǎng)絡(luò) 
  (flags & kSCNetworkReachabilityFlagsIsWWAN)    ? 'W' : '-', 
  //網(wǎng)絡(luò)是否可達(dá) 
  (flags & kSCNetworkReachabilityFlagsReachable)   ? 'R' : '-', 
  (flags & kSCNetworkReachabilityFlagsTransientConnection) ? 't' : '-', 
  (flags & kSCNetworkReachabilityFlagsConnectionRequired) ? 'c' : '-', 
  (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) ? 'C' : '-', 
  (flags & kSCNetworkReachabilityFlagsInterventionRequired) ? 'i' : '-', 
  (flags & kSCNetworkReachabilityFlagsConnectionOnDemand) ? 'D' : '-', 
  (flags & kSCNetworkReachabilityFlagsIsLocalAddress)  ? 'l' : '-', 
  (flags & kSCNetworkReachabilityFlagsIsDirect)   ? 'd' : '-', 
  comment 
  ); 
#endif 
} 
static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info) 
{ 
#pragma unused (target, flags) 
 NSCAssert(info != NULL, @"info was NULL in ReachabilityCallback"); 
 NSCAssert([(__bridge NSObject*) info isKindOfClass: [Reachability class]], @"info was wrong class in ReachabilityCallback"); 
 //http://www.cnblogs.com/xiaofeixiang 
 Reachability* noteObject = (__bridge Reachability *)info; 
 // Post a notification to notify the client that the network reachability changed. 
 [[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject]; 
} 
#pragma mark - Reachability implementation 
@implementation Reachability 
{ 
 BOOL _alwaysReturnLocalWiFiStatus; //default is NO 
 SCNetworkReachabilityRef _reachabilityRef; 
} 
//通過(guò)域名進(jìn)行實(shí)例化 博客園-Fly_Elephant 
+ (instancetype)reachabilityWithHostName:(NSString *)hostName 
{ 
 Reachability* returnValue = NULL; 
 SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, [hostName UTF8String]); 
 if (reachability != NULL) 
 { 
 returnValue= [[self alloc] init]; 
 if (returnValue != NULL) 
 { 
  returnValue->_reachabilityRef = reachability; 
  returnValue->_alwaysReturnLocalWiFiStatus = NO; 
 } 
 } 
 return returnValue; 
} 
//通過(guò)ip地址實(shí)例化Reachability 
+ (instancetype)reachabilityWithAddress:(const struct sockaddr_in *)hostAddress 
{ 
 SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, (const struct sockaddr *)hostAddress); 
 Reachability* returnValue = NULL; 
 if (reachability != NULL) 
 { 
 returnValue = [[self alloc] init]; 
 if (returnValue != NULL) 
 { 
  returnValue->_reachabilityRef = reachability; 
  returnValue->_alwaysReturnLocalWiFiStatus = NO; 
 } 
 } 
 return returnValue; 
} 
//檢測(cè)是否能夠直接連上互聯(lián)網(wǎng) 
+ (instancetype)reachabilityForInternetConnection 
{ 
 struct sockaddr_in zeroAddress; 
 bzero(&zeroAddress, sizeof(zeroAddress)); 
 zeroAddress.sin_len = sizeof(zeroAddress); 
 zeroAddress.sin_family = AF_INET; 
 return [self reachabilityWithAddress:&zeroAddress]; 
} 
//檢測(cè)當(dāng)前網(wǎng)絡(luò)是否能夠聯(lián)上wifi 
+ (instancetype)reachabilityForLocalWiFi 
{ 
 struct sockaddr_in localWifiAddress; 
 bzero(&localWifiAddress, sizeof(localWifiAddress)); 
 localWifiAddress.sin_len = sizeof(localWifiAddress); 
 localWifiAddress.sin_family = AF_INET; 
 // IN_LINKLOCALNETNUM is defined in <netinet/in.h> as 169.254.0.0. 
 localWifiAddress.sin_addr.s_addr = htonl(IN_LINKLOCALNETNUM); 
 Reachability* returnValue = [self reachabilityWithAddress: &localWifiAddress]; 
 if (returnValue != NULL) 
 { 
 returnValue->_alwaysReturnLocalWiFiStatus = YES; 
 } 
 return returnValue; 
} 
#pragma mark - Start and stop notifier 
- (BOOL)startNotifier 
{ 
 BOOL returnValue = NO; 
 SCNetworkReachabilityContext context = {0, (__bridge voidvoid *)(self), NULL, NULL, NULL}; 
 //SCNetworkReachabilitySetCallback函數(shù)為指定一個(gè)target 
 //當(dāng)設(shè)備對(duì)于這個(gè)target鏈接狀態(tài)發(fā)生改變時(shí)(比如斷開(kāi)鏈接,或者重新連上),則回調(diào)reachabilityCallback函數(shù), 
 if (SCNetworkReachabilitySetCallback(_reachabilityRef, ReachabilityCallback, &context)) 
 { 
 if (SCNetworkReachabilityScheduleWithRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode)) 
 { 
  returnValue = YES; 
 } 
 } 
 return returnValue; 
} 
- (void)stopNotifier 
{ 
 if (_reachabilityRef != NULL) 
 { 
 SCNetworkReachabilityUnscheduleFromRunLoop(_reachabilityRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); 
 } 
} 
- (void)dealloc 
{ 
 [self stopNotifier]; 
 if (_reachabilityRef != NULL) 
 { 
 CFRelease(_reachabilityRef); 
 } 
} 
#pragma mark - Network Flag Handling 
- (NetworkStatus)localWiFiStatusForFlags:(SCNetworkReachabilityFlags)flags 
{ 
 PrintReachabilityFlags(flags, "localWiFiStatusForFlags"); 
 NetworkStatus returnValue = NotReachable; 
 if ((flags & kSCNetworkReachabilityFlagsReachable) && (flags & kSCNetworkReachabilityFlagsIsDirect)) 
 { 
 returnValue = ReachableViaWiFi; 
 } 
 return returnValue; 
} 
- (NetworkStatus)networkStatusForFlags:(SCNetworkReachabilityFlags)flags 
{ 
 PrintReachabilityFlags(flags, "networkStatusForFlags"); 
 if ((flags & kSCNetworkReachabilityFlagsReachable) == 0) 
 { 
 // The target host is not reachable. 
 return NotReachable; 
 } 
 NetworkStatus returnValue = NotReachable; 
 if ((flags & kSCNetworkReachabilityFlagsConnectionRequired) == 0) 
 { 
 /* 
  If the target host is reachable and no connection is required then we'll assume (for now) that you're on Wi-Fi... 
  */ 
 returnValue = ReachableViaWiFi; 
 } 
 if ((((flags & kSCNetworkReachabilityFlagsConnectionOnDemand ) != 0) || 
 (flags & kSCNetworkReachabilityFlagsConnectionOnTraffic) != 0)) 
 { 
 /* 
  ... and the connection is on-demand (or on-traffic) if the calling application is using the CFSocketStream or higher APIs... 
  */ 
 if ((flags & kSCNetworkReachabilityFlagsInterventionRequired) == 0) 
 { 
  /* 
  ... and no [user] intervention is needed... 
  */ 
  returnValue = ReachableViaWiFi; 
 } 
 } 
 if ((flags & kSCNetworkReachabilityFlagsIsWWAN) == kSCNetworkReachabilityFlagsIsWWAN) 
 { 
 /* 
  ... but WWAN connections are OK if the calling application is using the CFNetwork APIs. 
  */ 
 returnValue = ReachableViaWWAN; 
 } 
 return returnValue; 
} 
- (BOOL)connectionRequired 
{ 
 NSAssert(_reachabilityRef != NULL, @"connectionRequired called with NULL reachabilityRef"); 
 SCNetworkReachabilityFlags flags; 
 if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags)) 
 { 
 return (flags & kSCNetworkReachabilityFlagsConnectionRequired); 
 } 
 return NO; 
} 
//獲取當(dāng)前網(wǎng)絡(luò)狀態(tài) 
- (NetworkStatus)currentReachabilityStatus 
{ 
 NSAssert(_reachabilityRef != NULL, @"currentNetworkStatus called with NULL SCNetworkReachabilityRef"); 
 NetworkStatus returnValue = NotReachable; 
 SCNetworkReachabilityFlags flags; 
 if (SCNetworkReachabilityGetFlags(_reachabilityRef, &flags)) 
 { 
 if (_alwaysReturnLocalWiFiStatus) 
 { 
  returnValue = [self localWiFiStatusForFlags:flags]; 
 } 
 else 
 { 
  returnValue = [self networkStatusForFlags:flags]; 
 } 
 } 
 return returnValue; 
} 
@end

AppDelegate中的實(shí)現(xiàn):

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
 //添加一個(gè)系統(tǒng)通知 
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityChanged:) name:kReachabilityChangedNotification object:nil]; 
 //初始化 
 self.internetReachability=[Reachability reachabilityForInternetConnection]; 
 //通知添加到Run Loop 
 [self.internetReachability startNotifier]; 
 [self updateInterfaceWithReachability:_internetReachability]; 
 return YES; 
} 

回調(diào)函數(shù):

(void) reachabilityChanged:(NSNotification *)note 
{ 
 Reachability* curReach = [note object]; 
 NSParameterAssert([curReach isKindOfClass:[Reachability class]]); 
 [self updateInterfaceWithReachability:curReach]; 
} 
- (void)updateInterfaceWithReachability:(Reachability *)reachability 
{ 
 NetworkStatus netStatus = [reachability currentReachabilityStatus]; 
 switch (netStatus) { 
 case NotReachable: 
  NSLog(@"====當(dāng)前網(wǎng)絡(luò)狀態(tài)不可達(dá)=======http://www.cnblogs.com/xiaofeixiang"); 
  break; 
 case ReachableViaWiFi: 
  NSLog(@"====當(dāng)前網(wǎng)絡(luò)狀態(tài)為Wifi=======博客園-Fly_Elephant"); 
  break; 
 case ReachableViaWWAN: 
  NSLog(@"====當(dāng)前網(wǎng)絡(luò)狀態(tài)為3G=======keso"); 
  break; 
 } 
} 

總結(jié)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

相關(guān)文章

  • iOS 11 UINavigationItem 去除左右間隙的方法

    iOS 11 UINavigationItem 去除左右間隙的方法

    本篇文章主要介紹了iOS 11 UINavigationItem 去除左右間隙的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-10-10
  • iOS 圖片旋轉(zhuǎn)方法實(shí)例代碼

    iOS 圖片旋轉(zhuǎn)方法實(shí)例代碼

    這篇文章主要介紹了iOS 圖片旋轉(zhuǎn)方法實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-03-03
  • iOS內(nèi)存管理中引用計(jì)數(shù)的學(xué)習(xí)

    iOS內(nèi)存管理中引用計(jì)數(shù)的學(xué)習(xí)

    文章給大家分享了關(guān)于iOS內(nèi)存管理中引用計(jì)數(shù)的相關(guān)知識(shí)點(diǎn),對(duì)此有需要的朋友可以跟著學(xué)習(xí)下。
    2018-05-05
  • IOS開(kāi)發(fā)之路--C語(yǔ)言預(yù)處理

    IOS開(kāi)發(fā)之路--C語(yǔ)言預(yù)處理

    由于預(yù)處理指令是在編譯之前就進(jìn)行了,因此很多時(shí)候它要比在程序運(yùn)行時(shí)進(jìn)行操作效率高。在C語(yǔ)言中包括三類預(yù)處理指令,今天將一一介紹:宏定義、條件編譯、文件包含
    2014-08-08
  • IOS設(shè)置按鈕為圓角的示例代碼

    IOS設(shè)置按鈕為圓角的示例代碼

    這篇文章給大家分享了IOS按鈕設(shè)置為圓角的方法,按鈕的四個(gè)角都可隨意設(shè)置為圓角,對(duì)大家開(kāi)發(fā)IOS具有一定的參考借鑒價(jià)值。有需要的朋友們可以參考借鑒。
    2016-09-09
  • iOS如何獲取屏幕寬高、設(shè)備型號(hào)、系統(tǒng)版本信息

    iOS如何獲取屏幕寬高、設(shè)備型號(hào)、系統(tǒng)版本信息

    這篇文章主要介紹了iOS如何獲取屏幕寬高、設(shè)備型號(hào)、系統(tǒng)版本信息的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Flutter?Widgets之標(biāo)簽類控件Chip詳解

    Flutter?Widgets之標(biāo)簽類控件Chip詳解

    這篇文章主要為大家介紹了Flutter?Widgets之標(biāo)簽類控件Chip詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-10-10
  • iOS仿微信圖片分享界面實(shí)現(xiàn)代碼

    iOS仿微信圖片分享界面實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了iOS仿微信相冊(cè)界面翻轉(zhuǎn)過(guò)渡動(dòng)畫(huà)效果,微信采用界面翻轉(zhuǎn)的過(guò)渡動(dòng)畫(huà)跳轉(zhuǎn)到評(píng)論界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • iOS開(kāi)發(fā)實(shí)現(xiàn)轉(zhuǎn)盤功能

    iOS開(kāi)發(fā)實(shí)現(xiàn)轉(zhuǎn)盤功能

    這篇文章主要為大家詳細(xì)介紹了iOS開(kāi)發(fā)實(shí)現(xiàn)轉(zhuǎn)盤功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • iOS實(shí)現(xiàn)左右可滑動(dòng)的選擇條實(shí)例代碼分享

    iOS實(shí)現(xiàn)左右可滑動(dòng)的選擇條實(shí)例代碼分享

    本文通過(guò)實(shí)例代碼給大家介紹了ios實(shí)現(xiàn)左右可滑動(dòng)的選擇條功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下
    2017-03-03

最新評(píng)論

彭州市| 花莲县| 离岛区| 犍为县| 黄平县| 永春县| 景宁| 临桂县| 沈丘县| 辽阳县| 昭觉县| 嘉定区| 青川县| 抚顺县| 五莲县| 无锡市| 耒阳市| 彭山县| 青龙| 大港区| 鄱阳县| 秭归县| 柳林县| 天台县| 临朐县| 古蔺县| 阳春市| 黔西县| 蒙自县| 锦屏县| 库车县| 淮南市| 肥东县| 松江区| 开鲁县| 景东| 师宗县| 涿鹿县| 军事| 寿阳县| 鱼台县|