有没有一种程序化的方式来了解 iPhone 所使用的蜂窝运营商?
我正在寻找 iPhone 连接的运营商名称。
有没有一种程序化的方式来了解 iPhone 所使用的蜂窝运营商?
我正在寻找 iPhone 连接的运营商名称。
iOS 4版本中,可以使用CoreTelephony框架,以下是获取运营商名称的代码片段:
CTTelephonyNetworkInfo *netinfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [netinfo subscriberCellularProvider];
NSLog(@"Carrier Name: %@", [carrier carrierName]);
[netinfo release];
链接 CoreTelephony 并将其包含在您的头文件中:
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>
仅做记录..我在不同的SIM卡上测试了这个API,似乎iPhone锁定的运营商名称是通过[carrer carrierName]返回的!!
我在两台iPhone上测试了这一点,一台是被锁定的,另一台没有,对于被锁定的那一台,无论SIM卡提供商如何,每次运行我的测试应用程序都会返回它所锁定的运营商名称。但请注意,MNC确实会改变!
CTCarrier
,carrierName
等信息已在iOS 16中被废弃且没有替代品:https://developer.apple.com/documentation/coretelephony/ctcarrier。
import CoreTelephony
static var carrierName:String? {
let networkInfo = CTTelephonyNetworkInfo()
let carrier = networkInfo.subscriberCellularProvider
return carrier?.carrierName
}
VVCarrierParameters.h
似乎有一个carrierServiceName
类方法,可能是您所需的。将该头文件添加到您的项目中,并调用[VVCarrierParameters carrierServiceName]
。在开发 Alpha时,我遇到了同样的问题。该项目本身不限于仅使用公共API,因此首先尝试了@Jason Harwig的解决方案。由于我无法使其工作,所以考虑了另一个选项。
我的解决方案使用私有API访问标签(UIStatusBarServiceItemView
)中显示在状态栏中的_serviceString
ivar。
它依赖于状态栏具有载体值,并且只需要UIKit
即可工作。
- (NSString *)carrierName
{
UIView* statusBar = [self statusBar];
UIView* statusBarForegroundView = nil;
for (UIView* view in statusBar.subviews)
{
if ([view isKindOfClass:NSClassFromString(@"UIStatusBarForegroundView")])
{
statusBarForegroundView = view;
break;
}
}
UIView* statusBarServiceItem = nil;
for (UIView* view in statusBarForegroundView.subviews)
{
if ([view isKindOfClass:NSClassFromString(@"UIStatusBarServiceItemView")])
{
statusBarServiceItem = view;
break;
}
}
if (statusBarServiceItem)
{
id value = [statusBarServiceItem valueForKey:@"_serviceString"];
if ([value isKindOfClass:[NSString class]])
{
return (NSString *)value;
}
}
return @"Unavailable";
}
- (UIView *)statusBar
{
NSString *statusBarString = [NSString stringWithFormat:@"%@ar", @"_statusB"];
return [[UIApplication sharedApplication] valueForKey:statusBarString];
}
func getCarrierName() -> String? {
var carrierName: String?
let typeName: (Any) -> String = { String(describing: type(of: $0)) }
let statusBar = UIApplication.shared.value(forKey: "_statusBar") as! UIView
for statusBarForegroundView in statusBar.subviews {
if typeName(statusBarForegroundView) == "UIStatusBarForegroundView" {
for statusBarItem in statusBarForegroundView.subviews {
if typeName(statusBarItem) == "UIStatusBarServiceItemView" {
carrierName = (statusBarItem.value(forKey: "_serviceString") as! String)
}
}
}
}
return carrierName
}
如果您使用的是Swift和iOS 12.0及以下版本,请执行以下操作:
导入CoreTelephony
static var carrierName:String? {
CTTelephonyNetworkInfo().serviceSubscriberCellularProviders?.first?.value.carrierName ?? ""
}
当您打印carrier?.description的输出时
这是您看到的:
[\"0000000100000001\": CTCarrier (0x2803a1980) {\n\t运营商名称:[Vodafone]\n\t移动国家代码:[214]\n\t移动网络代码:[01]\n\tISO国家代码:[es]\n\t允许VOIP?[是]\n}\n]
格式化后(\n和\t):
[\"0000000100000001\": CTCarrier (0x2803a1980) {
Carrier name: [Vodafone]
Mobile Country Code: [214]
Mobile Network Code:[01]
ISO Country Code:[es]
Allows VOIP? [YES]
}
]
从状态栏获取运营商名称是一个不错的选择(至少对我来说是这样)
我的意思是“codethemall”用户的答案。
有这样一种方法,但它仅适用于iOS 4,因此您无法在早期版本上使用它。而且这可能会破坏您的向后兼容性。