如何接收有关连接类型已更改(3G,Edge,Wifi,GPRS)的通知 [英] How do I receive notifications that the connection has changed type (3G, Edge, Wifi, GPRS)

查看:91
本文介绍了如何接收有关连接类型已更改(3G,Edge,Wifi,GPRS)的通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以发现当前连接是3G,Edge还是WiFi,并接收有关此消息的通知?或者只是一种发现连接是否为WiFi的方法?

Is there a way to discover whether the current connection is 3G, Edge or WiFi, and receive a notification of this? Or just a way do discover whether the connection is WiFi?

推荐答案

您应该使用Apple的Reachability类

You should use Apple's Reachability class

http://developer.apple.com/library /ios/#samplecode/Reachability/Listings/Classes_Reachability_h.html

实施后,您可以在委托中使用它:

after implementation you can use this in the delegate:

- (void) configureTextField:  (Reachability*) curReach
{
    NetworkStatus netStatus = [curReach currentReachabilityStatus];
    BOOL connectionRequired= [curReach connectionRequired];
    NSString* statusString= @"non";
    switch (netStatus)
    {
        case NotReachable:
        {
            statusString = @"Access Not Available";

            //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";


            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Cellular Data Detected" message:@"You are using Cellular data such as 3G or Edge. Downloading large amount of data may effect your cellular internet package costs. To avoid such extra cost kindly use Wifi." 
                                                           delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
            [alert release];


            break;
        }
        case ReachableViaWiFi:
        {
            statusString= @"Reachable WiFi";

            break;
        }
    }
    if(connectionRequired)
    {
        statusString= [NSString stringWithFormat: @"%@, Connection Required", statusString];
    }
    NSLog(@"Network= %@",statusString);
    if ([statusString isEqualToString:@"Access Not Available"]){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Internet Connection" message:@"It seems you are not connected to the internet, the app will try to load from the last cached data - assuming this data exist." 
                                                       delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    //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];

        [self configureTextField:curReach];

    }   
}

这篇关于如何接收有关连接类型已更改(3G,Edge,Wifi,GPRS)的通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆