You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS 16及以上系统SIM卡插入检测技术求助

iOS 16+ 设备SIM卡检测:权限与API可用性问题

我正在开发一款iOS应用,需要检测设备是否插入SIM卡,目标系统为iOS 16及以上版本。尝试使用CTTelephonyNetworkInfo与CTSubscriber接口时,遇到了权限及API可用性问题。

已尝试的操作

Info.plist 初始配置

已添加位置权限相关配置:

<key>NSLocationWhenInUseUsageDescription</key>
<string>我们需要访问您的位置信息以判断网络可用性。</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>我们需要访问您的位置信息以判断网络可用性。</string>

代码实现

import UIKit
import CoreTelephony
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    let locationManager = CLLocationManager()
    let networkInfo = CTTelephonyNetworkInfo()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 设置代理并请求位置服务权限
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        
        // 检查SIM卡状态
        let simStatus = checkSimAvailability()
        print(simStatus)
    }
    
    func checkSimAvailability() -> String {
        if #available(iOS 16.0, *) {
            let subscriber = CTSubscriber()
            if #available(iOS 18.0, *) {
                do {
                    if subscriber.isSIMInserted {
                        return "已插入SIM卡。"
                    } else {
                        return "未检测到SIM卡。"
                    }
                } catch {
                    return "检测SIM卡状态出错:\(error.localizedDescription)"
                }
            } else {
                return "isSIMInserted 仅在iOS 18.0及以上版本可用。"
            }
        } else {
            return "isSIMInserted 仅在iOS 16.0及以上版本可用。"
        }
        
        if let carriers = networkInfo.serviceSubscriberCellularProviders,
           let carrier = carriers.first?.value,
           let _ = carrier.mobileNetworkCode {
            return "SIM卡可用。\n运营商名称:\(carrier.carrierName ?? "未知")"
        } else {
            return "未安装SIM卡"
        }
    }

    // CLLocationManagerDelegate 代理方法
    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        switch status {
        case .authorizedWhenInUse, .authorizedAlways:
            print("位置服务已授权。")
        case .denied, .restricted:
            print("位置服务被拒绝或受限。")
        case .notDetermined:
            print("位置服务权限尚未确定。")
        @unknown default:
            print("未知状态。")
        }
    }
}

运行错误

在iOS 18 beta 3上运行代码时,抛出权限拒绝错误:

Error determining if SIM is inserted: Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"

补充的Info.plist配置

为解决权限问题,又添加了以下配置项:

<key>NSCTTelephonyUsageDescription</key>
<string>本应用需要访问蜂窝网络提供商信息以检测SIM卡是否存在。</string>

<key>SIMSupportedNetworks</key>
<array>
    <dict>
        <key>MCC</key>
        <string>525</string>
        <key>MNC</key>
        <string>01</string>
    </dict>
    <dict>
        <key>MCC</key>
        <string>525</string>
        <key>MNC</key>
        <string>02</string>
    </dict>
    <dict>
        <key>MCC</key>
        <string>525</string>
        <key>MNC</key>
        <string>07</string>
    </dict>
</array>
<key>com.apple.developer.coretelephony.sim-inserted</key>
<true/>

内容的提问来源于stack exchange,提问作者IamVariable

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 21:06:01