【轉】獲取CID 和 LAC的方法


原文地址: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 #else
27 
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 #else
15 
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版本以上所有解決方案都需要有權工作

 

<key>com.apple.CommCenter.fine-grained</key>
<array>
    <string>spi</string>
</array>

 

  不僅電池監控器是受保護的,但它似乎像所有CoreTelephony通知現在要求權利的工作。例如,kCTMessageReceivedNotification也受到影響。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM