一、使用公共API獲取MCC和MNC
在iOS中,我們可以使用CoreTelephony框架來獲取用戶的移動(dòng)國(guó)家代碼(MCC)和移動(dòng)網(wǎng)絡(luò)代碼(MNC)。具體操作步驟如下:
-
在Xcode項(xiàng)目中,點(diǎn)擊項(xiàng)目目標(biāo),進(jìn)入“General”選項(xiàng)卡,在“Frameworks, Libraries, and Embedded Content”下點(diǎn)擊“+”按鈕,搜索并添加
CoreTelephony.framework
。 -
在需要獲取MCC和MNC信息的Swift文件頂部導(dǎo)入CoreTelephony框架:
import CoreTelephony
- 創(chuàng)建一個(gè)
CTTelephonyNetworkInfo
實(shí)例:
let networkInfo = CTTelephonyNetworkInfo()
- 使用
CTTelephonyNetworkInfo
實(shí)例的serviceSubscriberCellularProviders
屬性獲取包含CTCarrier
對(duì)象的字典。字典中的每個(gè)鍵都對(duì)應(yīng)用戶設(shè)備中的一個(gè)運(yùn)營(yíng)商:
if let carrierInfo = networkInfo.serviceSubscriberCellularProviders {
for (key, carrier) in carrierInfo {
// 獲取每個(gè)運(yùn)營(yíng)商的MCC和MNC
let mobileCountryCode = carrier.mobileCountryCode
let mobileNetworkCode = carrier.mobileNetworkCode
print("Carrier: \(key), MCC: \(String(describing: mobileCountryCode)), MNC: \(String(describing: mobileNetworkCode))")
}
}
CTCarrier
實(shí)例的mobileCountryCode
和mobileNetworkCode
屬性將給出MCC和MNC值。需要注意的是,如果設(shè)備沒有SIM卡、處于飛行模式或未連接到蜂窩網(wǎng)絡(luò),此方法可能返回nil
。在應(yīng)用程序中,應(yīng)適當(dāng)處理這些情況。
二、iOS 16中CTCarrier被棄用后的替代方案
根據(jù)Apple 開發(fā)者文檔中描述,CTCarrier在iOS 16.0中會(huì)被取消。
經(jīng)過了iOS16.1-iOS 16.4,這一天終于來了。新版本Xcode 14.3 打包,在iOS 16.4以上,CTCarrier的屬性都被禁用了,直接返回65535
或者- -
。
@available(iOS, introduced: 4.0, deprecated: 16.0, message: "Deprecated with no replacement")
open class CTCarrier : NSObject {
/*
* carrierName
*
* Discussion:
* An NSString containing the name of the subscriber's cellular service provider.
*/
@available(iOS, introduced: 4.0, deprecated: 16.0, message: "Deprecated; returns '--' at some point in the future")
open var carrierName: String? { get }
/*
* mobileCountryCode
*
* Discussion:
* An NSString containing the mobile country code for the subscriber's
* cellular service provider, in its numeric representation
*/
@available(iOS, introduced: 4.0, deprecated: 16.0, message: "Deprecated; returns '65535' at some point in the future")
open var mobileCountryCode: String? { get }
/*
* mobileNetworkCode
*
* Discussion:
* An NSString containing the mobile network code for the subscriber's
* cellular service provider, in its numeric representation
*/
@available(iOS, introduced: 4.0, deprecated: 16.0, message: "Deprecated; returns '65535' at some point in the future")
open var mobileNetworkCode: String? { get }
/*
* isoCountryCode
*
* Discussion:
* Returns an NSString object that contains country code for
* the subscriber's cellular service provider, represented as an ISO 3166-1
* country code string
*/
@available(iOS, introduced: 4.0, deprecated: 16.0, message: "Deprecated; returns '--' at some point in the future")
open var isoCountryCode: String? { get }
/*
* allowsVOIP
*
* Discussion:
* A BOOL value that is YES if this carrier allows VOIP calls to be
* made on its network, NO otherwise.
*/
@available(iOS, introduced: 4.0, deprecated: 16.0, message: "Deprecated; returns YES at some point in the future")
open var allowsVOIP: Bool { get }
}
若要使用私有API,可以按照以下步驟操作:
(請(qǐng)注意,使用私有API可能導(dǎo)致應(yīng)用被拒絕,且這些API可能在未來的iOS版本中發(fā)生變化或被移除。因此,在可能的情況下,最好依賴于公共API。)
API被棄用,蘋果可能會(huì)提供替代的公共API。
要使用私有API,可以按照以下步驟操作:
-
首先,在Swift項(xiàng)目中創(chuàng)建一個(gè)橋接頭文件,以便訪問Objective-C函數(shù)。具體操作如下:
a. File > New > File > Header File,將其命名為BridgingHeader.h
。
b. 轉(zhuǎn)到項(xiàng)目目標(biāo)的Build Settings選項(xiàng)卡,搜索“Objective-C Bridging Header”并設(shè)置值為BridgingHeader.h
文件的路徑。路徑應(yīng)該類似于:$(PROJECT_DIR)/YourProjectName/BridgingHeader.h
。 -
將以下代碼添加到
BridgingHeader.h
文件中,以暴露私有API:
#import <CoreTelephony/CoreTelephonyDefines.h>
__BEGIN_DECLS
extern NSString* const CTRadioAccessTechnologyDidChangeNotification;
extern NSString* const CTSubscriberInfo;
CFNotificationCenterRef _CTServerConnectionCreate(CFAllocatorRef, void(*)(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo), int*);
void _CTServerConnectionAddToRunLoop(CFNotificationCenterRef, CFRunLoopRef, CFStringRef);
void _CTServerConnectionSetTargetQueue(CFNotificationCenterRef, dispatch_queue_t);
CTTelephonyCenterAddObserver(id, CFNotificationCallback, CFStringRef, void *, CFNotificationSuspensionBehavior);
__END_DECLS
- 在Swift文件中導(dǎo)入CoreTelephony框架和橋接頭文件:
import CoreTelephony
import Foundation
- 定義一個(gè)函數(shù),當(dāng)CTCarrier發(fā)生變化時(shí)將被調(diào)用:
func radioAccessTechnologyDidChange(notification: Notification) {
if let userInfo = notification.userInfo as? [String: AnyObject],
let servingCell = userInfo["kCTIndicatorsGradedServingCellDescription"] as? [String: AnyObject],
let mcc = servingCell["MCC"] as? Int,
let mnc = servingCell["MNC"] as? Int {
print("MCC: \(mcc), MNC: \(mnc)")
}
}
- 設(shè)置回調(diào)并為CTCarrier變化通知添加觀察者:
let notificationCenter = CFNotificationCenterGetDarwinNotifyCenter()
let callback: CFNotificationCallback = { center, observer, name, object, userInfo in
let notification = Notification(name: Notification.Name(rawValue: name! as String), object: nil, userInfo: userInfo as? [AnyHashable: Any])
radioAccessTechnologyDidChange(notification: notification)
}
let notificationName = "kCTIndicatorsSignalStrengthNotification" as CFString
CFNotificationCenterAddObserver(notificationCenter, nil, callback, notificationName, nil, .deliverImmediately)
三、嘗試混淆私有API獲取
使用私有API時(shí),為了規(guī)避App Store審核,可以嘗試混淆私有API的調(diào)用。請(qǐng)注意,這種做法并不能保證你的應(yīng)用能夠通過審核,因?yàn)樘O果可能會(huì)采用其他方法來檢測(cè)私有API的使用。此外,依賴私有API可能導(dǎo)致應(yīng)用在未來的iOS版本中出現(xiàn)兼容性問題。
以下是一個(gè)嘗試混淆私有API調(diào)用的例子:
-
首先,確保已經(jīng)按照前面的說明創(chuàng)建了一個(gè)橋接頭文件(
BridgingHeader.h
)。 -
在
BridgingHeader.h
文件中,將私有API的聲明改為動(dòng)態(tài)獲取。刪除原有的私有API聲明,并添加以下代碼:文章來源:http://www.zghlxwxcb.cn/news/detail-501826.html
#import <CoreTelephony/CoreTelephonyDefines.h>
__BEGIN_DECLS
CFNotificationCenterRef (*_CTServerConnectionCreate)(CFAllocatorRef, void(*)(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo), int*);
void (*_CTServerConnectionAddToRunLoop)(CFNotificationCenterRef, CFRunLoopRef, CFStringRef);
void (*_CTServerConnectionSetTargetQueue)(CFNotificationCenterRef, dispatch_queue_t);
CTTelephonyCenterAddObserver(id, CFNotificationCallback, CFStringRef, void *, CFNotificationSuspensionBehavior);
__END_DECLS
- 在Swift文件中,使用動(dòng)態(tài)庫(kù)加載(
dlopen
)和符號(hào)查找(dlsym
)來獲取私有API的函數(shù)指針。確保已經(jīng)導(dǎo)入了CoreTelephony
框架和Foundation
框架:
import CoreTelephony
import Foundation
- 實(shí)現(xiàn)一個(gè)方法來獲取私有API的函數(shù)指針:
func getPrivateAPIFunctionPointers() {
let coreTelephonyHandle = dlopen("/System/Library/Frameworks/CoreTelephony.framework/CoreTelephony", RTLD_LAZY)
if coreTelephonyHandle != nil {
let createFunction = dlsym(coreTelephonyHandle, "_CTServerConnectionCreate")
let addToRunLoopFunction = dlsym(coreTelephonyHandle, "_CTServerConnectionAddToRunLoop")
let setTargetQueueFunction = dlsym(coreTelephonyHandle, "_CTServerConnectionSetTargetQueue")
_CTServerConnectionCreate = unsafeBitCast(createFunction, to: type(of: _CTServerConnectionCreate))
_CTServerConnectionAddToRunLoop = unsafeBitCast(addToRunLoopFunction, to: type(of: _CTServerConnectionAddToRunLoop))
_CTServerConnectionSetTargetQueue = unsafeBitCast(setTargetQueueFunction, to: type(of: _CTServerConnectionSetTargetQueue))
dlclose(coreTelephonyHandle)
}
}
- 在需要獲取MCC和MNC的地方,調(diào)用
getPrivateAPIFunctionPointers()
方法來獲取私有API的函數(shù)指針。然后按照之前的步驟設(shè)置回調(diào)和添加觀察者。
請(qǐng)注意,盡管這種方法可以在一定程度上混淆私有API的使用,但無法保證應(yīng)用能夠通過App Store的審核。此外,依賴私有API可能導(dǎo)致應(yīng)用在未來的iOS版本中出現(xiàn)兼容性問題。文章來源地址http://www.zghlxwxcb.cn/news/detail-501826.html
到了這里,關(guān)于iOS中獲取MCC和MNC的方法及iOS 16中CTCarrier被棄用的替代方案的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!