简体   繁体   English

如何确定用户是在iPhone上的Edge还是3G

[英]How to determine whether user is on Edge or 3G on iPhone

Though it is relatively straightforward to determine if an iPhone is on Wifi or a data network programmatically in your application, I can't figure out a way to determine if the iPhone is on Edge or 3G. 虽然在应用程序中以程序方式确定iPhone是否在Wifi或数据网络上是相对简单的,但我无法找到确定iPhone是在Edge还是3G上的方法。

Anybody figure out a way to determine this? 有人想办法确定这个吗?

Note: Not worried about Apple AppStore acceptance policies so I don't mind doing something hacky in my app. 注意:不担心Apple AppStore接受政策,所以我不介意在我的应用程序中做一些hacky。 (The iPhones should not have to be jailbroken though) (虽然iPhone不应该被越狱)

The iPhone doesn't provide this kind of information to developers programmatically. iPhone不以编程方式向开发人员提供此类信息。 The best you can hope for is to determine whether a connection to a given host will have to be routed over a cell network - see the SCNetworkReachability reference and the Reachability project for more. 您可以期望的最好方法是确定是否必须通过单元网络路由到给定主机的连接 - 有关详细信息,请参阅SCNetworkReachability参考和Reachability项目。

假设Tim的答案是正确的,你可以通过一种方式判断用户是处于3G还是边缘,你可以通过启动计时器来测试连接的速度,该计时器让应用程序从网上下载一些文件并计算速度,你应该能够通过速度的差异来判断它们是在3G还是Edge上。

Try checking it from the status bar: 尝试从状态栏检查它:

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

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

And the value keys I've found so far: 到目前为止我找到的价值键:

0 = No wifi or cellular

1 = 2G and earlier? (not confirmed)

2 = 3G? (not yet confirmed)

3 = 4G

4 = LTE

5 = Wifi

one way you can detect iPhone 2G for sure is to look at the device name as reported by the OS 一种可以检测iPhone 2G的方法是查看操作系统报告的设备名称

#import <sys/utsname.h>
+ (NSString *)deviceType {
    struct utsname u;
    uname(&u);
    NSString *returnValue = [NSString stringWithFormat:@"%s", u.machine];

    return returnValue;
}

the return value you are looking for is "iPhone1,1" to indicated iPhone 2G. 您正在寻找的返回值是指示iPhone 2G的“iPhone1,1”。 combine this with the Reachability project to tell when they're on a cell network and you you have 1 avenue to guarantee that they are on an edge connection 将它与Reachability项目结合起来,告诉他们什么时候他们在一个小区网络上,你有一条途径来保证他们在边缘连接上

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM