一、使用公共API获取MCC和MNC

在iOS中,我们可以使用CoreTelephony框架来获取用户的移动国家代码(MCC)和移动网络代码(MNC)。具体操作步骤如下:

在Xcode项目中,点击项目目标,进入“General”选项卡,在“Frameworks, Libraries, and Embedded Content”下点击“+”按钮,搜索并添加CoreTelephony.framework。 在需要获取MCC和MNC信息的Swift文件顶部导入CoreTelephony框架:

import CoreTelephony

创建一个CTTelephonyNetworkInfo实例:

let networkInfo = CTTelephonyNetworkInfo()

使用CTTelephonyNetworkInfo实例的serviceSubscriberCellularProviders属性获取包含CTCarrier对象的字典。字典中的每个键都对应用户设备中的一个运营商:

if let carrierInfo = networkInfo.serviceSubscriberCellularProviders {

for (key, carrier) in carrierInfo {

// 获取每个运营商的MCC和MNC

let mobileCountryCode = carrier.mobileCountryCode

let mobileNetworkCode = carrier.mobileNetworkCode

print("Carrier: \(key), MCC: \(String(describing: mobileCountryCode)), MNC: \(String(describing: mobileNetworkCode))")

}

}

CTCarrier实例的mobileCountryCode和mobileNetworkCode属性将给出MCC和MNC值。需要注意的是,如果设备没有SIM卡、处于飞行模式或未连接到蜂窝网络,此方法可能返回nil。在应用程序中,应适当处理这些情况。

二、iOS 16中CTCarrier被弃用后的替代方案

根据Apple 开发者文档中描述,CTCarrier在iOS 16.0中会被取消。 经过了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,可以按照以下步骤操作: (请注意,使用私有API可能导致应用被拒绝,且这些API可能在未来的iOS版本中发生变化或被移除。因此,在可能的情况下,最好依赖于公共API。)

API被弃用,苹果可能会提供替代的公共API。

要使用私有API,可以按照以下步骤操作:

首先,在Swift项目中创建一个桥接头文件,以便访问Objective-C函数。具体操作如下: a. File > New > File > Header File,将其命名为BridgingHeader.h。 b. 转到项目目标的Build Settings选项卡,搜索“Objective-C Bridging Header”并设置值为BridgingHeader.h文件的路径。路径应该类似于:$(PROJECT_DIR)/YourProjectName/BridgingHeader.h。 将以下代码添加到BridgingHeader.h文件中,以暴露私有API:

#import

__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文件中导入CoreTelephony框架和桥接头文件:

import CoreTelephony

import Foundation

定义一个函数,当CTCarrier发生变化时将被调用:

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)")

}

}

设置回调并为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时,为了规避App Store审核,可以尝试混淆私有API的调用。请注意,这种做法并不能保证你的应用能够通过审核,因为苹果可能会采用其他方法来检测私有API的使用。此外,依赖私有API可能导致应用在未来的iOS版本中出现兼容性问题。

以下是一个尝试混淆私有API调用的例子:

首先,确保已经按照前面的说明创建了一个桥接头文件(BridgingHeader.h)。 在BridgingHeader.h文件中,将私有API的声明改为动态获取。删除原有的私有API声明,并添加以下代码:

#import

__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文件中,使用动态库加载(dlopen)和符号查找(dlsym)来获取私有API的函数指针。确保已经导入了CoreTelephony框架和Foundation框架:

import CoreTelephony

import Foundation

实现一个方法来获取私有API的函数指针:

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的地方,调用getPrivateAPIFunctionPointers()方法来获取私有API的函数指针。然后按照之前的步骤设置回调和添加观察者。

请注意,尽管这种方法可以在一定程度上混淆私有API的使用,但无法保证应用能够通过App Store的审核。此外,依赖私有API可能导致应用在未来的iOS版本中出现兼容性问题。

精彩链接

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。