Friday, July 19, 2024
HomeiOS Developmentswift - Is there any strategy to detect SIM insertion on iOS...

swift – Is there any strategy to detect SIM insertion on iOS 16 and later?


I’m growing an iOS software the place I must detect if a SIM card is inserted within the system. I’m focusing on iOS 16 and later. I’ve tried utilizing CTTelephonyNetworkInfo and CTSubscriber, however I’m encountering points with permissions and API availability.

Here’s what I’ve tried to this point:

Data.plist Configuration
I’ve added the mandatory keys to my Data.plist:

<key>NSLocationWhenInUseUsageDescription</key>
<string>We'd like entry to your location to find out community availability.</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>We'd like entry to your location to find out community availability.</string>
import UIKit
import CoreTelephony
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

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

    override func viewDidLoad() {
        tremendous.viewDidLoad()
        
        // Set the delegate and request location providers permissions
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        
        // Verify SIM availability
        let simStatus = checkSimAvailability()
        print(simStatus)
    }
    
    func checkSimAvailability() -> String {
        if #out there(iOS 16.0, *) {
            let subscriber = CTSubscriber()
            if #out there(iOS 18.0, *) {
                do {
                    if subscriber.isSIMInserted {
                        return "SIM card is inserted."
                    } else {
                        return "No SIM card detected."
                    }
                } catch {
                    return "Error figuring out if SIM is inserted: (error.localizedDescription)"
                }
            } else {
                return "isSIMInserted is just out there on iOS 18.0 or newer."
            }
        } else {
            return "isSIMInserted is just out there on iOS 16.0 or newer."
        }
        
        if let carriers = networkInfo.serviceSubscriberCellularProviders,
           let service = carriers.first?.worth,
           let _ = service.mobileNetworkCode {
            return "SIM card is offered.nCarrier Identify: (service.carrierName ?? "None")"
        } else {
            return "No SIM card put in"
        }
    }

    // CLLocationManagerDelegate
    func locationManager(_ supervisor: CLLocationManager, didChangeAuthorization standing: CLAuthorizationStatus) {
        swap standing {
        case .authorizedWhenInUse, .authorizedAlways:
            print("Location providers licensed.")
        case .denied, .restricted:
            print("Location providers denied or restricted.")
        case .notDetermined:
            print("Location providers permission not decided but.")
        @unknown default:
            print("Unknown case.")
        }
    }
}

Once I run this code on iOS 18 beta 3, I get the next error:

Error figuring out if SIM is inserted: Error Area=NSPOSIXErrorDomain Code=13 “Permission denied”

As well as i additionally added the next in data.plist

<key>NSCTTelephonyUsageDescription</key>
    <string>This app requires entry to mobile supplier data to detect the presence of a SIM card.</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/>



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments