Objective-C确定iOS设备的数据网络类型 [英] Objective-C determine data network type of the iOS device

查看:185
本文介绍了Objective-C确定iOS设备的数据网络类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从服务器接收数据的应用程序上,问题是用户连接到移动数据(不是 3G WIFI ),接收数据需要很长时间。

Im on an application that receive data from server, the problem is when user connect to cellular data (Not 3G or WIFI), it take ages to receive data.

我已经从这个答案,但我不确定它是否有效,有时它给我一个准确的类型,有时它没有。

i had implemented this code from this Answer but im not sure if it is effective or not, sometimes it's giving me an accurate type, and sometimes it don't.

这是我的代码:

- (void)newtworkType {

NSArray *subviews = [[[[UIApplication sharedApplication] valueForKey:@"statusBar"] valueForKey:@"foregroundView"]subviews];
NSNumber *dataNetworkItemView = nil;

for (id subview in subviews) {
    if([subview isKindOfClass:[NSClassFromString(@"UIStatusBarDataNetworkItemView") class]]) {
        dataNetworkItemView = subview;
        break;
    }
}

switch ([[dataNetworkItemView valueForKey:@"dataNetworkType"]integerValue]) {
    case 0:
        NSLog(@"No wifi or cellular");
        break;

    case 1:
        NSLog(@"2G");
        break;

    case 2:
        NSLog(@"3G");
        break;

    case 3:
        NSLog(@"4G");
        break;

    case 4:
        NSLog(@"LTE");
        break;

    case 5:
        NSLog(@"Wifi");
        break;


    default:
        break;
}}

这是我能做的最好的??,我试过 Apple Reachability示例,但它可以确定是否 reachabilityForInternetConnection 或只是 reachabilityForLocalWiFi 但在我的情况下没有帮助。

is this the best i can do??, i tried Apple Reachability example, but it can determine if reachabilityForInternetConnection or just reachabilityForLocalWiFi but that not helpfull in my case.

在此先感谢。

推荐答案

确保状态栏不是隐藏在您的应用程序中
,如果它不可见,它将始终返回没有wifi或手机,因为你的代码读取状态栏中的文字那就是全部。

Make sure that the Status bar is not hidden in your application. if it's not visible it will always return No wifi or cellular because your code reads the text in the Status bar thats all.

这是解决问题的最佳方法,只需将状态栏隐藏起来然后应用程序将获得有关网络类型的文本。

this is the best way to solve your problem, just make the Status bar not hidden then the application will get the text about the network type.

这篇关于Objective-C确定iOS设备的数据网络类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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