博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【转】获取CID 和 LAC的方法
阅读量:5016 次
发布时间:2019-06-12

本文共 7054 字,大约阅读时间需要 23 分钟。

原文地址:http://stackoverflow.com/questions/13399659/get-cellid-mcc-mnc-lac-and-network-in-ios-5-1

在iOS5 ~ iOS8.3可是使用此方法,此处使用CoreTelephony.framework私有API。同时支持GSM和UMTS

1、使用电池监控器

1 struct CTResult 2 { 3     int flag; 4     int a; 5 }; 6  7 extern CFStringRef const kCTCellMonitorCellType; 8 extern CFStringRef const kCTCellMonitorCellTypeServing; 9 extern CFStringRef const kCTCellMonitorCellTypeNeighbor;10 extern CFStringRef const kCTCellMonitorCellId;11 extern CFStringRef const kCTCellMonitorLAC;12 extern CFStringRef const kCTCellMonitorMCC;13 extern CFStringRef const kCTCellMonitorMNC;14 extern CFStringRef const kCTCellMonitorUpdateNotification;15 16 id _CTServerConnectionCreate(CFAllocatorRef, void*, int*);17 void _CTServerConnectionAddToRunLoop(id, CFRunLoopRef, CFStringRef);18 19 #ifdef __LP64__20 21 void _CTServerConnectionRegisterForNotification(id, CFStringRef);22 void _CTServerConnectionCellMonitorStart(id);23 void _CTServerConnectionCellMonitorStop(id);24 void _CTServerConnectionCellMonitorCopyCellInfo(id, void*, CFArrayRef*);25 26 #else27 28 void _CTServerConnectionRegisterForNotification(struct CTResult*, id, CFStringRef);29 #define _CTServerConnectionRegisterForNotification(connection, notification) { struct CTResult res; _CTServerConnectionRegisterForNotification(&res, connection, notification); }30 31 void _CTServerConnectionCellMonitorStart(struct CTResult*, id);32 #define _CTServerConnectionCellMonitorStart(connection) { struct CTResult res; _CTServerConnectionCellMonitorStart(&res, connection); }33 34 void _CTServerConnectionCellMonitorStop(struct CTResult*, id);35 #define _CTServerConnectionCellMonitorStop(connection) { struct CTResult res; _CTServerConnectionCellMonitorStop(&res, connection); }36 37 void _CTServerConnectionCellMonitorCopyCellInfo(struct CTResult*, id, void*, CFArrayRef*);38 #define _CTServerConnectionCellMonitorCopyCellInfo(connection, tmp, cells) { struct CTResult res; _CTServerConnectionCellMonitorCopyCellInfo(&res, connection, tmp, cells); }39 40 #endif

 

1 id CTConnection = _CTServerConnectionCreate(NULL, CellMonitorCallback, NULL); 2 _CTServerConnectionAddToRunLoop(CTConnection, CFRunLoopGetCurrent(), kCFRunLoopCommonModes); 3 _CTServerConnectionRegisterForNotification(CTConnection, kCTCellMonitorUpdateNotification); 4 _CTServerConnectionCellMonitorStart(CTConnection); 5  6 int CellMonitorCallback(id connection, CFStringRef string, CFDictionaryRef dictionary, void *data) 7 { 8     int tmp = 0; 9     CFArrayRef cells = NULL;10     _CTServerConnectionCellMonitorCopyCellInfo(connection, (void*)&tmp, &cells);11     if (cells == NULL)12     {13         return 0;14     }15 16     for (NSDictionary* cell in (NSArray*)cells)17     {18         int LAC, CID, MCC, MNC;19 20         if ([cell[(NSString*)kCTCellMonitorCellType] isEqualToString:(NSString*)kCTCellMonitorCellTypeServing])21         {22             LAC = [cell[(NSString*)kCTCellMonitorLAC] intValue];23             CID = [cell[(NSString*)kCTCellMonitorCellId] intValue];24             MCC = [cell[(NSString*)kCTCellMonitorMCC] intValue];25             MNC = [cell[(NSString*)kCTCellMonitorMNC] intValue];26         }27         else if ([cell[(NSString*)kCTCellMonitorCellType] isEqualToString:(NSString*)kCTCellMonitorCellTypeNeighbor])28         {29         }30     }31 32     CFRelease(cells);33 34     return 0;35 }

2、使用CTTelephonyCenter

kCTRegistrationCellChangedNotification 每一个服务小区塔改变时被调用

1 extern CFStringRef const kCTRegistrationCellChangedNotification; 2 extern CFStringRef const kCTRegistrationGsmLac; 3 extern CFStringRef const kCTRegistrationLac; 4 extern CFStringRef const kCTRegistrationGsmCellId; 5 extern CFStringRef const kCTRegistrationCellId; 6  7 CFStringRef CTSIMSupportCopyMobileSubscriberCountryCode(CFAllocatorRef); 8 CFStringRef CTSIMSupportCopyMobileSubscriberNetworkCode(CFAllocatorRef); 9 10 id CTTelephonyCenterGetDefault();11 void CTTelephonyCenterAddObserver(id, void, CFNotificationCallback, CFStringRef, void, CFNotificationSuspensionBehavior);
1 CTTelephonyCenterAddObserver(CTTelephonyCenterGetDefault(), NULL, callback, NULL, NULL, CFNotificationSuspensionBehaviorHold); 2  3 void callback(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) 4 { 5     NSString* notification = (NSString*)name; 6     NSDictionary *cellInfo = (NSDictionary*)userInfo; 7  8     if ([notification isEqualToString:(NSString*)kCTRegistrationCellChangedNotification]) 9     {10         int LAC, CID, MCC, MNC;11 12         if (cellInfo[(NSString*)kCTRegistrationGsmLac])13         {14             LAC = [cellInfo[(NSString*)kCTRegistrationGsmLac] intValue];15         }16         else if (data[(NSString*)kCTRegistrationLac])17         {18             LAC = [cellInfo[(NSString*)kCTRegistrationLac] intValue];19         }20 21         if (cellInfo[(NSString*)kCTRegistrationGsmCellId])22         {23             CID = [cellInfo[(NSString*)kCTRegistrationGsmCellId] intValue];24         }25         else if (cellInfo[(NSString*)kCTRegistrationCellId])26         {27             CID = [cellInfo[(NSString*)kCTRegistrationCellId] intValue];28         }29 30         MCC = [[(NSString*)CTSIMSupportCopyMobileSubscriberCountryCode(NULL) autorelease] intValue];31         MNC = [[(NSString*)CTSIMSupportCopyMobileSubscriberNetworkCode(NULL) autorelease] intValue];32     }33 }

3、返回当前服务小区塔

1 struct CTResult 2 { 3     int flag; 4     int a; 5 }; 6  7 id _CTServerConnectionCreate(CFAllocatorRef, void*, int*); 8  9 #ifdef __LP64__10 11 void _CTServerConnectionGetLocationAreaCode(id, int*);12 void _CTServerConnectionGetCellID(id, int*);13 14 #else15 16 void _CTServerConnectionGetLocationAreaCode(struct CTResult*, id, int*);17 #define _CTServerConnectionGetLocationAreaCode(connection, LAC) { struct CTResult res; _CTServerConnectionGetLocationAreaCode(&res, connection, LAC); }18 19 void _CTServerConnectionGetCellID(struct CTResult*, id, int*);20 #define _CTServerConnectionGetCellID(connection, CID) { struct CTResult res; _CTServerConnectionGetCellID(&res, connection, CID); }21 22 #endif
1 int CID, LAC, MCC, MNC;2 3 id CTConnection = _CTServerConnectionCreate(NULL, NULL, NULL);4 _CTServerConnectionGetCellID(CTConnection, &CID);5 _CTServerConnectionGetLocationAreaCode(CTConnection, &LAC);6 MCC = [[(NSString*)CTSIMSupportCopyMobileSubscriberCountryCode(NULL) autorelease] intValue];7 MNC = [[(NSString*)CTSIMSupportCopyMobileSubscriberNetworkCode(NULL) autorelease] intValue];

更新数据

在ARM64(iPhone 5S)有与接受所有CoreTelephony功能的问题struct CTResult争论。显然,CoreTelephony的​​64位版本的出口这些功能没有struct CTResult说法。参数将是错误的-正因为如此,如果你调用这些函数像你过去那样你会得到ARM64一个错误。我更新函数声明,使他们在32位和64位ARM架构工作。我测试了它和它的作品在两个iPhone 4S的和iPhone 5S。

这仅适用于ARM64。如果你建立你的项目为32位ARM架构则没有这样的问题。您的应用程序将使用CoreTelephony的​​32位版本,预计struct CTResult说法。

8.3更新

着iOS 8.3版本以上所有解决方案都需要有权工作

 

com.apple.CommCenter.fine-grained
spi

 

  不仅电池监控器是受保护的,但它似乎像所有CoreTelephony通知现在要求权利的工作。例如,kCTMessageReceivedNotification也受到影响。

 

转载于:https://www.cnblogs.com/sleepingSun/p/5806378.html

你可能感兴趣的文章
SQLServer 基本语法
查看>>
Python入门基础知识(1) :locals() 和globals()
查看>>
python模块之multiprocessing模块, threading模块, concurrent.futures模块
查看>>
css-文字和图片在容器内垂直居中的简单方法
查看>>
杭电3784(继续xxx定律)
查看>>
PHP 的 HMAC_SHA1算法 实现
查看>>
深入理解javascript原型和闭包_____全部
查看>>
2016年中国的SaaS服务商企业研究
查看>>
HTML5:离线存储(缓存机制)-IndexDB
查看>>
9-5
查看>>
Laxcus大数据管理系统2.0(5)- 第二章 数据组织
查看>>
kafka入门样例 for java
查看>>
Redis存储AccessToken
查看>>
Use commons-email-1.1.jar+activation.jar+mail.jar to send Email
查看>>
hdu 2160 Sequence one(DFS)
查看>>
ATM实验感受
查看>>
csharp基础
查看>>
hdu4497 正整数唯一分解定理应用
查看>>
html5 拖曳功能的实现[转]
查看>>
[BZOJ 2049] [Sdoi2008] Cave 洞穴勘测 【LCT】
查看>>