如何接收连接类型已更改的通知(3G、Edge、Wifi、GPRS)

发布于 2024-12-08 18:35:57 字数 65 浏览 4 评论 0原文

有没有办法发现当前连接是 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风柔一江水 2024-12-15 18:35:57

您应该使用 Apple 的 Reachability 类

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

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

- (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];

    }   
}

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];

    }   
}
慵挽 2024-12-15 18:35:57

是的,苹果公司有一个示例项目可以证明这一点。 可达性

这是一个示例。

Yes, Apple has an example project that shows that. Reachability

And here's an example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文