Is there any way to determine if the iphone is roaming?

北城以北 提交于 2019-11-26 18:43:18

There is! It's not documented at all, and I highly doubt this would work on a non-jailbroken phone (as it requires using files not in the sandbox). However, here is how it is done.

The iPhone file system keeps two softlinks:

static NSString *carrierPListSymLinkPath = @"/var/mobile/Library/Preferences/com.apple.carrier.plist";
static NSString *operatorPListSymLinkPath = @"/var/mobile/Library/Preferences/com.apple.operator.plist";

when these links are pointing at the same file, the telephone is not roaming. When pointing at different files, the telephone is romaing.

Simplified code (no error checking, etc):

- (BOOL)isRoaming
{
    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *error;
    NSString *carrierPListPath = [fm destinationOfSymbolicLinkAtPath:carrierPListSymLinkPath error:&error];
    NSString *operatorPListPath = [fm destinationOfSymbolicLinkAtPath:operatorPListSymLinkPath error:&error];
    return (![operatorPListPath isEqualToString:carrierPListPath]);
}
Trinca

the solution of the symlinks are not the only way to do it but definitely it is the best. As I just realized, the Strings returned contains the MCC and MNC codes of the operator and carrier!!! Even the core telephony framework is not able to retrieve those informations about the operator your iPhone is attached when in roaming.

 Logs: 
 carrier: /System/Library/Carrier Bundles/iPhone/72410/carrier.plist
 operator: /System/Library/Carrier Bundles/iPhone/20810/carrier.plist

As you can see, the carrier (original cellular provider) line contains the file inside the "folder" 72410, which means MCC 724 (Brazil) and MNC 10 (VIVO). The operator (actually the one my cell phone is attached now - i'm in roaming) is inside the folder 20810, which means MCC 208 (France) and MNC 10 (SFR).

By the way, I'm using iPhone 4 with iOS5.

On a non-jailbreak device you can use third party services like http://ipinfo.io (my own service) to find out the current country of even carrier code based on the device's IP address, and you can then compare that to the CTCarrier details to determine if the device is roaming. Here's the standard ipinfo.io API response:

$ curl ipinfo.io/24.32.148.1 
{
    "ip": "24.32.148.1",
    "hostname": "doc-24-32-148-1.pecos.tx.cebridge.net",
    "city": "Pecos",
    "region": "Texas",
    "country": "US",
    "loc": "31.3086,-103.5892",
    "org": "AS7018 AT&T Services, Inc.",
    "postal": "79772"
}

Custom packages are available that also include the mnc/mcc details of mobile IPs though. See http://ipinfo.io/developers for details.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!