国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

IOS判斷網絡環境

2019-11-14 19:38:28
字體:
來源:轉載
供稿:網友

https://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html

我下載的是vertion2.2

 

 

開發Web等網絡應用程序的時候,需要確認網絡環境,連接情況等信息。如果沒有處理它們,是不會通過Apple的審查的。

Reachability

Apple 的 例程 Reachability 中介紹了取得/檢測網絡狀態的方法。

在你的程序中使用

1、Reachability 只須將該例程中的 Reachability.h 和 Reachability.m 拷貝到你的工程中; 2、然后將 SystemConfiguration.framework 添加進工程。

Reachability 中定義了3種網絡狀態。

typedefenum {

NotReachable = 0,//無連接

ReachableViaWiFi,//使用3G/GPRS網絡

ReachableViaWWAN//使用WiFi網絡

 

} NetworkStatus;

 

簡單判斷項目連接:

// 是否wifi

+ (BOOL) IsEnableWIFI {

return ([[Reachability reachabilityForLocalWiFi] currentReachabilityStatus] != NotReachable);

}

// 是否網絡連接

+ (BOOL) IsEnableConnection {

return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] != NotReachable);

}

 

// 是否網絡連接

+ (BOOL) IsEnable3G {

return ([[Reachability reachabilityForInternetConnection] currentReachabilityStatus] == ReachableViaWWAN);

}

 

 

下面是監聽整個項目網絡例子:

java代碼 復制代碼 收藏代碼
  1. /* 
  2.  
  3. File: ReachabilityAppDelegate.m 
  4. Abstract: The application's controller. 
  5.  
  6. Version: 2.2 
  7.  
  8. Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Inc. 
  9. ("Apple") in consideration of your agreement to the following terms, and your 
  10. use, installation, modification or redistribution of this Apple software 
  11. constitutes acceptance of these terms.  If you do not agree with these terms, 
  12. please do not use, install, modify or redistribute this Apple software. 
  13.  
  14. In consideration of your agreement to abide by the following terms, and subject 
  15. to these terms, Apple grants you a personal, non-exclusive license, under 
  16. Apple's copyrights in this original Apple software (the "Apple Software"), to 
  17. use, reproduce, modify and redistribute the Apple Software, with or without 
  18. modifications, in source and/or binary forms; provided that if you redistribute 
  19. the Apple Software in its entirety and without modifications, you must retain 
  20. this notice and the following text and disclaimers in all such redistributions 
  21. of the Apple Software. 
  22. Neither the name, trademarks, service marks or logos of Apple Inc. may be used 
  23. to endorse or promote products derived from the Apple Software without specific 
  24. prior written permission from Apple.  Except as expressly stated in this notice, 
  25. no other rights or licenses, express or implied, are granted by Apple herein, 
  26. including but not limited to any patent rights that may be infringed by your 
  27. derivative works or by other works in which the Apple Software may be 
  28. incorporated. 
  29.  
  30. The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO 
  31. WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED 
  32. WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  33. PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OperaTION ALONE OR IN 
  34. COMBINATION WITH YOUR PRODUCTS. 
  35.  
  36. IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR 
  37. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 
  38. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  39. ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR 
  40. DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF 
  41. CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF 
  42. APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
  43.  
  44. Copyright (C) 2010 Apple Inc. All Rights Reserved. 
  45.  
  46. */  
  47.   
  48. #import "ReachabilityAppDelegate.h"  
  49. #import "Reachability.h"  
  50.   
  51. @implementation ReachabilityAppDelegate  
  52.   
  53. - (void) configureTextField: (UITextField*) textField imageView: (UIImageView*) imageView reachability: (Reachability*) curReach  
  54. {  
  55.     NetworkStatus netStatus = [curReach currentReachabilityStatus];  
  56.     BOOL connectionRequired= [curReach connectionRequired];  
  57.     NSString* statusString= @"";  
  58.     switch (netStatus)  
  59.     {  
  60.         case NotReachable:  
  61.         {  
  62.             statusString = @"access Not Available";  
  63.             imageView.image = [UIImage imageNamed: @"stop-32.png"] ;  
  64.             //Minor interface detail- connectionRequired may return yes, even when the host is unreachable.  We cover that up here...  
  65.             connectionRequired= NO;    
  66.             break;  
  67.         }  
  68.               
  69.         case ReachableViaWWAN:  
  70.         {  
  71.             statusString = @"Reachable WWAN";  
  72.             imageView.image = [UIImage imageNamed: @"WWAN5.png"];  
  73.             break;  
  74.         }  
  75.         case ReachableViaWiFi:  
  76.         {  
  77.              statusString= @"Reachable WiFi";  
  78.             imageView.image = [UIImage imageNamed: @"Airport.png"];  
  79.             break;  
  80.       }  
  81.     }  
  82.     if(connectionRequired)  
  83.     {  
  84.         statusString= [NSString stringWithFormat: @"%@, Connection Required", statusString];  
  85.     }  
  86.     textField.text= statusString;  
  87. }  
  88.   
  89. - (void) updateInterfaceWithReachability: (Reachability*) curReach  
  90. {  
  91.     if(curReach == hostReach)  
  92.     {  
  93.         [self configureTextField: remoteHostStatusField imageView: remoteHostIcon reachability: curReach];  
  94.         NetworkStatus netStatus = [curReach currentReachabilityStatus];  
  95.         BOOL connectionRequired= [curReach connectionRequired];  
  96.   
  97.         summaryLabel.hidden =(netStatus != ReachableViaWWAN);  
  98.         NSString* baseLabel=  @"";  
  99.         if(connectionRequired)  
  100.         {  
  101.             baseLabel=  @"Cellular data network is available./n  Internet traffic will be routed through it after a connection is established.";  
  102.         }  
  103.         else  
  104.         {  
  105.             baseLabel=  @"Cellular data network is active./n  Internet traffic will be routed through it.";  
  106.         }  
  107.         summaryLabel.text= baseLabel;  
  108.     }  
  109.     if(curReach == internetReach)  
  110.     {     
  111.         [self configureTextField: internetConnectionStatusField imageView: internetConnectionIcon reachability: curReach];  
  112.     }  
  113.     if(curReach == wifiReach)  
  114.     {     
  115.         [self configureTextField: localWiFiConnectionStatusField imageView: localWiFiConnectionIcon reachability: curReach];  
  116.     }  
  117.       
  118. }  
  119.   
  120. //Called by Reachability whenever status changes.  
  121. - (void) reachabilityChanged: (NSNotification* )note  
  122. {  
  123.     Reachability* curReach = [note object];  
  124.     NSParameterAssert([curReach isKindOfClass: [Reachability class]]);  
  125.     [self updateInterfaceWithReachability: curReach];  
  126. }  
  127.   
  128.   
  129. - (void) applicationDidFinishLaunching: (UIApplication* )application   
  130. {  
  131.     #pragma unused(application)  
  132.     contentView.backgroundColor = [UIColor groupTableViewBackgroundColor];  
  133.       
  134.     summaryLabel.hidden = YES;          
  135.       
  136.   
  137.     // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the  
  138.     // method "reachabilityChanged" will be called.   
  139.     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];  
  140.   
  141.     //Change the host name here to change the server your monitoring  
  142.     remoteHostLabel.text = [NSString stringWithFormat: @"Remote Host: %@", @"www.apple.com"];  
  143.     hostReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];  
  144.     [hostReach startNotifier];  
  145.     [self updateInterfaceWithReachability: hostReach];  
  146.       
  147.     internetReach = [[Reachability reachabilityForInternetConnection] retain];  
  148.     [internetReach startNotifier];  
  149.     [self updateInterfaceWithReachability: internetReach];  
  150.   
  151.     wifiReach = [[Reachability reachabilityForLocalWiFi] retain];  
  152.     [wifiReach startNotifier];  
  153.     [self updateInterfaceWithReachability: wifiReach];  
  154.   
  155.     [window makeKeyAndVisible];  
  156.       
  157. }  
  158. @end  
/*File: ReachabilityAppDelegate.mAbstract: The application's controller.Version: 2.2Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple Inc.("Apple") in consideration of your agreement to the following terms, and youruse, installation, modification or redistribution of this Apple softwareconstitutes acceptance of these terms.  If you do not agree with these terms,please do not use, install, modify or redistribute this Apple software.In consideration of your agreement to abide by the following terms, and subjectto these terms, Apple grants you a personal, non-exclusive license, underApple's copyrights in this original Apple software (the "Apple Software"), touse, reproduce, modify and redistribute the Apple Software, with or withoutmodifications, in source and/or binary forms; provided that if you redistributethe Apple Software in its entirety and without modifications, you must retainthis notice and the following text and disclaimers in all such redistributionsof the Apple Software.Neither the name, trademarks, service marks or logos of Apple Inc. may be usedto endorse or promote products derived from the Apple Software without specificprior written permission from Apple.  Except as expressly stated in this notice,no other rights or licenses, express or implied, are granted by Apple herein,including but not limited to any patent rights that may be infringed by yourderivative works or by other works in which the Apple Software may beincorporated.The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NOWARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIEDWARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULARPURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR INCOMBINATION WITH YOUR PRODUCTS.IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL ORCONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTEGOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/ORDISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OFCONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IFAPPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.Copyright (C) 2010 Apple Inc. All Rights Reserved.*/#import "ReachabilityAppDelegate.h"#import "Reachability.h"@implementation ReachabilityAppDelegate- (void) configureTextField: (UITextField*) textField imageView: (UIImageView*) imageView reachability: (Reachability*) curReach{    NetworkStatus netStatus = [curReach currentReachabilityStatus];    BOOL connectionRequired= [curReach connectionRequired];    NSString* statusString= @"";    switch (netStatus)    {        case NotReachable:        {            statusString = @"Access Not Available";            imageView.image = [UIImage imageNamed: @"stop-32.png"] ;            //Minor interface detail- connectionRequired may return yes, even when the host is unreachable.  We cover that up here...            connectionRequired= NO;              break;        }                    case ReachableViaWWAN:        {            statusString = @"Reachable WWAN";            imageView.image = [UIImage imageNamed: @"WWAN5.png"];            break;        }        case ReachableViaWiFi:        {             statusString= @"Reachable WiFi";            imageView.image = [UIImage imageNamed: @"Airport.png"];            break;      }    }    if(connectionRequired)    {        statusString= [NSString stringWithFormat: @"%@, Connection Required", statusString];    }    textField.text= statusString;}- (void) updateInterfaceWithReachability: (Reachability*) curReach{    if(curReach == hostReach)	{		[self configureTextField: remoteHostStatusField imageView: remoteHostIcon reachability: curReach];        NetworkStatus netStatus = [curReach currentReachabilityStatus];        BOOL connectionRequired= [curReach connectionRequired];        summaryLabel.hidden =(netStatus != ReachableViaWWAN);        NSString* baseLabel=  @"";        if(connectionRequired)        {            baseLabel=  @"Cellular data network is available./n  Internet traffic will be routed through it after a connection is established.";        }        else        {            baseLabel=  @"Cellular data network is active./n  Internet traffic will be routed through it.";        }        summaryLabel.text= baseLabel;    }	if(curReach == internetReach)	{			[self configureTextField: internetConnectionStatusField imageView: internetConnectionIcon reachability: curReach];	}	if(curReach == wifiReach)	{			[self configureTextField: localWiFiConnectionStatusField imageView: localWiFiConnectionIcon reachability: curReach];	}	}//Called by Reachability whenever status changes.- (void) reachabilityChanged: (NSNotification* )note{	Reachability* curReach = [note object];	NSParameterAssert([curReach isKindOfClass: [Reachability class]]);	[self updateInterfaceWithReachability: curReach];}- (void) applicationDidFinishLaunching: (UIApplication* )application {    #pragma unused(application)	contentView.backgroundColor = [UIColor groupTableViewBackgroundColor];        summaryLabel.hidden = YES;                // Observe the kNetworkReachabilityChangedNotification. When that notification is posted, the    // method "reachabilityChanged" will be called.     [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabilityChanged:) name: kReachabilityChangedNotification object: nil];    //Change the host name here to change the server your monitoring    remoteHostLabel.text = [NSString stringWithFormat: @"Remote Host: %@", @"www.apple.com"];	hostReach = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];	[hostReach startNotifier];	[self updateInterfaceWithReachability: hostReach];	    internetReach = [[Reachability reachabilityForInternetConnection] retain];	[internetReach startNotifier];	[self updateInterfaceWithReachability: internetReach];    wifiReach = [[Reachability reachabilityForLocalWiFi] retain];	[wifiReach startNotifier];	[self updateInterfaceWithReachability: wifiReach];	[window makeKeyAndVisible];    }@end

 Reachability.zip


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 获嘉县| 利川市| 齐齐哈尔市| 元阳县| 通海县| 沁水县| 正蓝旗| 大化| 饶河县| 贵港市| 洛阳市| 辽阳县| 连山| 榆社县| 樟树市| 渝中区| 民乐县| 梓潼县| 绵阳市| 诏安县| 遵义县| 嫩江县| 澄江县| 潢川县| 隆昌县| 大同市| 桐柏县| 潍坊市| 谢通门县| 营山县| 柞水县| 墨玉县| 汉中市| 克山县| 七台河市| 精河县| 二连浩特市| 望江县| 西盟| 哈密市| 阳城县|