当前位置:编程学习 > wap >>

iphone开发使用Reachability判断网络状态

复制里面的 Reachability.h 和 Reachability.m 到项目中


2、添加 framework:

将 SystemConfiguration.framework 添加进工程。

在这里描述一下示例中的代码

下面的代码设置UITextField的值以及显示的图片。


[cpp]
- (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) 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;
}
在这里可以更UITextField的显示,以及图片的显示


[cpp]
- (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) 
&n

补充:移动开发 , IOS ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,