IOS——获取当前运营商(获取漫游接入网络的运营商)
来源:互联网 发布:星际淘宝网起点 编辑:程序博客网 时间:2024/04/30 15:01
这个问题坑了很久了,终于搞定了 —。—|||
如果使用CoreTelephony.framework 框架,获取的仅仅是Sim卡的运营商(而且还不成功!carrier.name属性为Carrier,坑死人了!)
那么思路稍微转一下,当手机漫游的时候,会接入当地的运营商,在手机Status Bar上会显示出来,那么只要获得Status Bar上显示的名字即可获得漫游手机接入网络的运营商了。
(1)进入Rumtime模式
#import <objc/message.h>
(2)进入实现代码:
要知道最终获取到状态栏上的运营商名称要经过一层层的提取,下面就开始一层层分析
1. 获得标题栏并遍历其中的所有元素//状态栏是由当前app控制的,首先获取当前app
UIApplication *app = [UIApplicationsharedApplication];
id statusBar = [appvalueForKeyPath:@"statusBar"];
// 遍历状态栏的所有成员
unsignedint outCount =0;
Ivar *ivars =class_copyIvarList([statusBarclass], &outCount);
for (int i =0; i < outCount; i++) {
Ivar ivar = ivars[i];
printf("%s\n",ivar_getName(ivar));
}
打印出的元素有:
_styleDelegate
_statusBarWindow
_statusBarServer
_showsForeground
_backgroundView
_foregroundView
_doubleHeightLabel
_currentDoubleHeightText
_currentRawData
_interruptedAnimationCompositeViews
_newStyleBackgroundView
_newStyleForegroundView
_slidingStatusBar
_requestedStyle
_styleOverrides
_style
_orientation
_hidden
_suppressesHiddenSideEffects
_foreground
_registered
_styleRequestedWhileHidden
_waitingOnCallbackAfterChangingStyleOverridesLocally
_suppressGlow
_translucentBackgroundAlpha
_showOnlyCenterItems
_localDataOverrides
_tintColor
_lastUsedTintColor
_nextTintTransition
2. 进入foregroundView元素并遍历其所有子元素
NSArray *children = [[[appvalueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
for (id childin children) {
NSLog(@"%@", [childclass]);
}
打印结果如下:
UIStatusBarSignalStrengthItemView
UIStatusBarServiceItemView
UIStatusBarDataNetworkItemView
UIStatusBarBatteryItemView
UIStatusBarBatteryPercentItemView
UIStatusBarTimeItemView
3. 进入UIStatusBarServiceItemView元素并遍历其所有子元素
NSArray *children = [[[appvalueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
for (id childin children) {
if ([childisKindOfClass:NSClassFromString(@"UIStatusBarServiceItemView")]) {
unsignedint outCount =0;
Ivar *ivars =class_copyIvarList([childclass], &outCount);
for (int i =0; i < outCount; i++) {
Ivar ivar = ivars[i];
printf("%s\n",ivar_getName(ivar));
}
}
}
打印结果如下:
_pathToServiceImages
_serviceString
_crossfadeString
_crossfadeStep
_maxWidth
_serviceWidth
_crossfadeWidth
_contentType
_loopingNecessaryForString
_usingStringForStyle
_loopNowIfNecessary
_letterSpacing
4. 通过serviceString获取运营商名称
NSArray *children = [[[appvalueForKeyPath:@"statusBar"]valueForKeyPath:@"foregroundView"]subviews];
for (id childin children) {
if ([childisKindOfClass:NSClassFromString(@"UIStatusBarServiceItemView")]) {
//遍历当前状态栏的所有属性,找到关于状态栏的
id type = [child valueForKeyPath:@"serviceString"];
NSLog(@"carrier is %@", type);
break;
}
}
打印结果:carrier is中国联通
总结一下,获取标题栏上的运营商的名称主要过程为:
statusBar -> foregroundView -> UIStatusBarServiceItemView -> serviceString -> 运营商名字
最后贴一下代码供大家参考
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 状态栏是由当前app控制的,首先获取当前app UIApplication *app = [UIApplication sharedApplication]; NSArray *children = [[[app valueForKeyPath:@"statusBar"] valueForKeyPath:@"foregroundView"] subviews]; for (id child in children) { if ([child isKindOfClass:NSClassFromString(@"UIStatusBarServiceItemView")]) { id type = [child valueForKeyPath:@"serviceString"]; NSLog(@"carrier is %@", type); break; } } return YES;}
- IOS——获取当前运营商(获取漫游接入网络的运营商)
- ios获取运营商和网络的demo
- ios获取运营商和网络的demo
- iOS-获取当前设备的信息,网络状态,运营商,设备震动.获取设备信息
- 使用 CoreTelephony 框架获取当前网络运营商
- iOS获取当前设备的型号、分辨率、运营商以及地理位置
- 获取网络运营商
- iOS获取网络类型和运营商信息
- ios如何获取手机的网络状态和运营商名称
- ios如何获取手机的网络状态和运营商名称
- ios如何获取手机的网络状态和运营商名称
- ios中获取手机的网络状态和运营商名称
- ios如何判断当前网络的运营商
- iOS如何判断当前网络的运营商
- iOS如何判断当前网络的运营商
- iOS如何判断当前网络的运营商
- iOS如何判断当前网络的运营商
- ios如何判断当前网络的运营商
- wpa_cli调试工具的使用
- sql 字段截取匹配的方法
- 抽屉效果功能实现
- 7,嵌入式Linux之系统时钟和UART实验
- 让IE支持HTML5
- IOS——获取当前运营商(获取漫游接入网络的运营商)
- 【jsp】script标签属性使用type还是language?
- C#设计模式之1——简单工厂模式
- 【Linux】Linux用户、用户组、文件权限学习笔记
- asp.net简介
- 5种php加密工具zendGuard、ionCube、SourceCop、SourceGuardian、phpShield
- linux下创建与删除用户详细步骤
- Android DrawerLayout抽屉效果
- android开源项目:图片下载缓存库picasso