Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> iOS

Không thể bật/tắt Data Connection (3G/4G/5G/WiFi) bằng code trong iOS

Không thể bật/tắt kết nối dữ liệu di động (Mobile Data) hoặc WiFi bằng lập trình (programmatically) trên iOS non-jailbreak.

Tại sao không thể?

  • Apple **không cung cấp public API** để kiểm soát radio hardware (Cellular, WiFi, Bluetooth)
  • Quyền kiểm soát network thuộc về **System / User** qua Settings
  • Sandbox: App không thể can thiệp cài đặt hệ thống cấp thấp
  • Private API tồn tại nhưng **chắc chắn bị reject App Store**

Private API (Tham khảo - KHÔNG dùng production)

  • CTTelephonyNetworkInfo (chỉ đọc info, không control)
  • Private frameworks: MobileWiFi.framework, CoreTelephony private methods
  • Jailbreak: SBSettings, Activator, MobileSubstrate tweaks

Điều app CÓ THỂ làm (Public API)

1. Kiểm tra trạng thái mạng


import Network
import SystemConfiguration.CaptiveNetwork

// Network framework (iOS 12+) - Khuyên dùng
let monitor = NWPathMonitor()
monitor.pathUpdateHandler = { path in
    if path.status == .satisfied {
        print("Có mạng: \(path.availableInterfaces)")
        if path.usesInterfaceType(.cellular) { print("Dùng 4G/5G") }
        if path.usesInterfaceType(.wifi) { print("Dùng WiFi") }
    } else {
        print("Mất kết nối mạng")
    }
}
let queue = DispatchQueue(label: "NetworkMonitor")
monitor.start(queue: queue)

2. Lấy tên WiFi đang kết nối (CaptiveNetwork - Deprecated iOS 13+)


import SystemConfiguration.CaptiveNetwork

func getCurrentWiFiSSID() -> String? {
    guard let interfaces = CNCopySupportedInterfaces() as? [String] else { return nil }
    for interface in interfaces {
        if let info = CNCopyCurrentNetworkInfo(interface as CFString) as? [String: Any],
           let ssid = info[kCFNetworkInfoKeySSID as String] as? String {
            return ssid
        }
    }
    return nil
}

Lưu ý: iOS 13+ cần capability "Access WiFi Information" + user permission location khi app foreground.

3. Mở Settings để user tự bật/tắt


// Mở cài đặt WiFi
UIApplication.shared.open(URL(string: "App-Prefs:root=WIFI")!)

// Mở cài đặt Cellular
UIApplication.shared.open(URL(string: "App-Prefs:root=MOBILE_DATA_SETTINGS_ID")!)

// Mở cài đặt chung
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)

4. Yêu cầu network cho task cụ thể (iOS 13+)


// Chỉ chạy task khi có mạng
let request = URLRequest(url: url)
request.networkServiceType = .responsiveData // Ưu tiên network

// Hoặc dùng URLSessionConfiguration
let config = URLSessionConfiguration.default
config.waitsForConnectivity = true // Tự chờ network available
config.timeoutIntervalForResource = 300

Kết luận

Không thể bật/tắt Mobile Data/WiFi bằng code. Chỉ có thể:

  1. Giám sát trạng thái mạng (NWPathMonitor)
  2. Đọc tên WiFi (có limitation)
  3. Mở Settings deep link nhờ user tự bật/tắt
  4. Thiết kế app offline-first, sync khi có mạng

Mọi thư viện/code claim làm được trên non-jailbreak device đều dùng Private API → **Reject 100%**.