一、使用公共API获取MCC和MNC
在iOS中,我们可以使用CoreTelephony框架来获取用户的移动国家代码(MCC)和移动网络代码(MNC)。具体操作步骤如下:
-
在Xcode项目中,点击项目目标,进入“General”选项卡,在“Frameworks, Libraries, and Embedded Content”下点击“+”按钮,搜索并添加
CoreTelephony.framework
。
import CoreTelephony
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。)
-
首先,在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
。
#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
import CoreTelephony
import Foundation
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)")
}
}
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版本中出现兼容性问题。
#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
import CoreTelephony
import Foundation
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)
}
}
请注意,尽管这种方法可以在一定程度上混淆私有API的使用,但无法保证应用能够通过App Store的审核。此外,依赖私有API可能导致应用在未来的iOS版本中出现兼容性问题。
原文地址:https://blog.csdn.net/sinat_15735647/article/details/130577032
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_14983.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!