How to determine whether user is on Edge or 3G on

2020-02-11 03:53发布

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.

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. (The iPhones should not have to be jailbroken though)

标签: iphone
4条回答
做自己的国王
2楼-- · 2020-02-11 04:03

one way you can detect iPhone 2G for sure is to look at the device name as reported by the OS

#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. 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

查看更多
Anthone
3楼-- · 2020-02-11 04:07

The iPhone doesn't provide this kind of information to developers programmatically. 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.

查看更多
Summer. ? 凉城
4楼-- · 2020-02-11 04:23

Assuming the answer by Tim is right, one way you can tell if the user is on 3G or edge is you can test the speed of the connection by starting a timer having the app download some file from the web and calculate the speed, you should be able to tell if they are on 3G or Edge by the diffrence in speeds.

查看更多
做自己的国王
5楼-- · 2020-02-11 04:25

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
查看更多
登录 后发表回答