IOS: 如何连接 - 断开移动数据

IOS: How to connect - disconnect Mobile Data

我想构建一个企业 IOS (未在 AppStore 中发布) 应用程序,使用户能够:

  1. 切换飞行模式On/Off

或者如果以上不可能

  1. 切换移动数据模式On/Off 我知道,为了做到这一点,我需要在 developers.apple.com 为我的特定应用程序 ID 启用权利。

授予我执行此类操作权限的 library/classes 是什么?

据我所知,您不能直接从应用程序中打开 on/off 蜂窝网络,我已经看到应用程序通过使用警报将用户移至该选项的设置来多次这样做。

  1. 有可能,但 Apple 没有针对它的特定库。你可以为它写一个扩展

  1. 有可能,苹果也有专门的库

     import Core Telephony // framework
    
     class CTCellularData : NSObject {  } // class
    
     enum CTCellularDataRestrictedState : UInt //  the case .restricted that will deny access to cellular data.
    

工作原理 ?

/* create a manager class and make a singleton instance. Check your reachability and handle your state */

class SKManager {

  static let shared = SKManager() 
  private let cellularData = CTCellularData()
  
  private var currentCellularDataState: CTCellularDataRestrictedState = .restrictedStateUnknown

// create this kind of method for checking your state :

func checkReachability(viewController: UIViewController?, completion: (_ restricted: Bool) -> Void) {

let isRestricted = currentCellularDataState == .restricted
    
    guard !isRestricted else {
         // you can trigger a pop up here if you wish
      }
      completion(true)
      return
    }
    completion(false)
  }

}

func handle() {
SKManager.shared.checkApiReachability(viewController: self) { (restricted) in
            if !restricted {
              // you can do not restricted issues here
            } else {
            // your network restricted, so this is your point which you want
         }
      }
    }