本文介绍: 本文主要介绍iOS开发如何获取移动国家代码(MCC)和移动网络代码(MNC),并探讨了在iOS 16中CTCarrier被弃用后的替代方案。我们将首先介绍使用公共API的获取方法然后讨论使用私有API的替代方案。

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

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

  1. 在Xcode项目中,点击项目目标进入“General”选项卡,在“Frameworks, Libraries, and Embedded Content”下点击“+”按钮搜索添加CoreTelephony.framework

  2. 需要获取MCC和MNC信息的Swift文件顶部导入CoreTelephony框架

import CoreTelephony
  1. 创建一个CTTelephonyNetworkInfo实例
let networkInfo = CTTelephonyNetworkInfo()
  1. 使用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实例mobileCountryCodemobileNetworkCode属性将给出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,可以按照以下步骤操作

  1. 首先,在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

  2. 将以下代码添加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
  1. 在Swift文件导入CoreTelephony框架桥接头文件
import CoreTelephony
import Foundation
  1. 定义一个函数,当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)")
    }
}
  1. 设置回调并为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调用的例子

  1. 首先,确保已经按照前面说明创建了一个桥接头文件BridgingHeader.h)。

  2. BridgingHeader.h文件中,将私有API的声明改为动态获取。删除原有的私有API声明,并添加以下代码:

#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
  1. 在Swift文件中,使用动态加载dlopen)和符号查找dlsym)来获取私有API的函数指针。确保已经导入CoreTelephony框架Foundation框架
import CoreTelephony
import Foundation
  1. 实现一个方法来获取私有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)
    }
}
  1. 在需要获取MCC和MNC的地方,调用getPrivateAPIFunctionPointers()方法来获取私有API的函数指针然后按照之前的步骤设置回调和添加观察者

请注意,尽管这种方法可以在一定程度上混淆私有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进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注