国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

iOS中獲取MCC和MNC的方法及iOS 16中CTCarrier被棄用的替代方案

這篇具有很好參考價(jià)值的文章主要介紹了iOS中獲取MCC和MNC的方法及iOS 16中CTCarrier被棄用的替代方案。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

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

在iOS中,我們可以使用CoreTelephony框架來獲取用戶的移動(dòng)國(guó)家代碼(MCC)和移動(dòng)網(wǎng)絡(luò)代碼(MNC)。具體操作步驟如下:

  1. 在Xcode項(xiàng)目中,點(diǎn)擊項(xiàng)目目標(biāo),進(jìn)入“General”選項(xiàng)卡,在“Frameworks, Libraries, and Embedded Content”下點(diǎn)擊“+”按鈕,搜索并添加CoreTelephony.framework

  2. 在需要獲取MCC和MNC信息的Swift文件頂部導(dǎo)入CoreTelephony框架:

import CoreTelephony
  1. 創(chuàng)建一個(gè)CTTelephonyNetworkInfo實(shí)例:
let networkInfo = CTTelephonyNetworkInfo()
  1. 使用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í)例的mobileCountryCodemobileNetworkCode屬性將給出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,可以按照以下步驟操作:

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

  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文件中導(dǎo)入CoreTelephony框架和橋接頭文件:
import CoreTelephony
import Foundation
  1. 定義一個(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)")
    }
}
  1. 設(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)用的例子:

  1. 首先,確保已經(jīng)按照前面的說明創(chuàng)建了一個(gè)橋接頭文件(BridgingHeader.h)。

  2. BridgingHeader.h文件中,將私有API的聲明改為動(dòng)態(tài)獲取。刪除原有的私有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文件中,使用動(dòng)態(tài)庫(kù)加載(dlopen)和符號(hào)查找(dlsym)來獲取私有API的函數(shù)指針。確保已經(jīng)導(dǎo)入了CoreTelephony框架和Foundation框架:
import CoreTelephony
import Foundation
  1. 實(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)
    }
}
  1. 在需要獲取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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • WebSecurityConfigurerAdapter被棄用Spring Security基于組件化的配置和使用

    WebSecurityConfigurerAdapter被棄用Spring Security基于組件化的配置和使用

    在Spring Security 5.7及之后的版本中 WebSecurityConfigurerAdapter 將被啟用,安全框架將轉(zhuǎn)向基于組件的安全配置。 spring security官方文檔 Spring Security without the WebSecurityConfigurerAdapter 如果使用的Spring Boot版本高于低于2.7.0、Spring Security版本高于5.7,就會(huì)出現(xiàn)如下的提示: 1、被啟用的原因

    2024年02月02日
    瀏覽(23)
  • Resthighlevelclient被棄用后,ES 8.x 最新用法 java api

    一、ES 8.x 整合springBoot 1、導(dǎo)入依賴 2、創(chuàng)建 ElasticsearchClient 二、ES java api 1、搜索 2、單個(gè)插入(更新)文檔 3、批量插入(更新)文檔

    2024年02月14日
    瀏覽(21)
  • Unity打包APK錯(cuò)誤:‘a(chǎn)ndroid.enableR8‘選項(xiàng)已被棄用,不應(yīng)再使用

    Unity打包APK錯(cuò)誤:\\\'android.enableR8’選項(xiàng)已被棄用,不應(yīng)再使用 在Unity游戲開發(fā)中,我們經(jīng)常需要將游戲打包成APK文件以在Android設(shè)備上進(jìn)行測(cè)試或發(fā)布。然而,有時(shí)候在打包APK的過程中,可能會(huì)遇到一些錯(cuò)誤。其中一個(gè)常見的錯(cuò)誤是 “The option ‘a(chǎn)ndroid.enableR8’ is deprecated and sh

    2024年02月08日
    瀏覽(93)
  • Python錯(cuò)題集-7:DeprecationWarning: Conversion of an array with ndim(被棄用警告)

    Python錯(cuò)題集-7:DeprecationWarning: Conversion of an array with ndim(被棄用警告)

    DeprecationWarning: Conversion of an array with ndim 0 to a scalar is deprecated, and will error in future. Ensure you extract a single element from your array before performing this operation. (Deprecated NumPy 1.25.) ? X[i] = np.random.normal(loc=Ex, scale=np.abs(Enn), size=1) DeprecationWarning: Conversion of an array with ndim ?是一個(gè)警告,通常出

    2024年04月09日
    瀏覽(33)
  • Elasticsearch RestHighLevelClient 已標(biāo)記為被棄用 它的替代方案 Elasticsearch Java API Client 的基礎(chǔ)教程及遷移方案

    在Elasticsearch7.15版本之后,Elasticsearch官方將它的高級(jí)客戶端 RestHighLevelClient 標(biāo)記為棄用狀態(tài)。同時(shí)推出了全新的Java API客戶端 Elasticsearch Java API Client ,該客戶端也將在Elasticsearch8.0及以后版本中成為官方推薦使用的客戶端。 Elasticsearch Java API Client 支持除 Vector tile search API 和

    2024年01月16日
    瀏覽(21)
  • Elasticsearch8.x版本中RestHighLevelClient被棄用,新版本中全新的Java客戶端Elasticsearch Java API Client中常用API練習(xí)

    在Es7.15版本之后,es官方將它的高級(jí)客戶端RestHighLevelClient標(biāo)記為棄用狀態(tài)。同時(shí)推出了全新的java API客戶端Elasticsearch Java API Client,該客戶端也將在Elasticsearch8.0及以后版本中成為官方推薦使用的客戶端。 Elasticsearch Java API Client支持除Vector title search API和Find structure API之外的所有

    2023年04月08日
    瀏覽(24)
  • 升級(jí)iOS17后可以降級(jí)嗎?iOS17退回iOS16方法教程分享

    升級(jí)iOS17后可以降級(jí)嗎?iOS17退回iOS16方法教程分享

    iOS 17已上線幾天,從網(wǎng)上用戶的反饋和媒體機(jī)構(gòu)的報(bào)告來看,iOS17系統(tǒng)對(duì)舊機(jī)型來說并不友好,除了電池續(xù)航下降以外,占用大量?jī)?chǔ)存空間,BUG也不少。 蘋果于 9 月 7 日發(fā)布了 iOS 16.6.1 版本,如果升級(jí)iOS17后發(fā)現(xiàn)不適合自己,可參考以下方法退回到iOS16.6.1。 降級(jí)工具小編推薦

    2024年02月08日
    瀏覽(21)
  • iOS16圖標(biāo)文字陰影如何去掉?分享陰影不顯示的方法!

    iOS16圖標(biāo)文字陰影如何去掉?分享陰影不顯示的方法!

    更新到iOS16正式版的iPhone用戶,發(fā)現(xiàn)繼續(xù)使用自己喜歡的淺色壁紙時(shí), 每個(gè)APP圖標(biāo)下方文字都出現(xiàn)了陰影 。 如上圖中所示,iOS16的圖標(biāo)文字陰影,應(yīng)該是蘋果開發(fā)為了在 淺色背景下凸出APP名稱 而設(shè)計(jì)的。但部分iPhone用戶紛紛表示,這樣的UI設(shè)計(jì)確實(shí)無法接受。 iOS16淺色壁紙

    2024年01月21日
    瀏覽(13)
  • iOS性能測(cè)試方法-獲取手機(jī)內(nèi)存數(shù)據(jù)

    iOS性能測(cè)試方法-獲取手機(jī)內(nèi)存數(shù)據(jù)

    最近在研究如何獲取iOS手機(jī)性能數(shù)據(jù)(主要是內(nèi)存),看了一圈目前主流的方法主要是兩種: 1.阿里開源的iOS自動(dòng)化測(cè)試工具tidevice;2.xcode自帶的instruments; 1.cpu 正常在20%-40%左右,超過80%需要引起重視。 2.內(nèi)存 rss:私有內(nèi)存+所有共享內(nèi)存,pss:私有內(nèi)存+比例分配共享內(nèi)存,

    2024年02月07日
    瀏覽(19)
  • IOS UDID 6種方法在線獲取

    IOS UDID 6種方法在線獲取

    IOS UDID: iOS設(shè)備的唯一識(shí)別碼,每臺(tái)iOS設(shè)備都有一個(gè)獨(dú)一無二的編碼,這個(gè)編碼,就稱為識(shí)別碼,也叫做UDID(Unique?Device Identifier) 獲取方式: 1、蒲公英 link:蒲公英 | 一步快速獲取 iOS 設(shè)備的UDID (1)點(diǎn)擊蒲公英link (2)ios手機(jī)右上角下拉,選擇掃一掃,掃描二維碼 (3)

    2024年02月07日
    瀏覽(18)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包