【问题标题】:Ios app to work in intranet connection onlyIos 应用程序只能在 Intranet 连接中工作
【发布时间】:2018-02-12 09:32:31
【问题描述】:

我正在尝试制作一个 ios 应用程序,该应用程序必须仅在 Intranet 连接中工作,如果尝试从不同网络访问 api 端点,该应用程序应显示警报对话框。我尝试使用苹果的可达性代码.. 我没有主机名,但我的主机的 ip 案例场景:

当连接到不同的网络时,它说没有连接,api调用也失败..需要什么

但是当连接到同一个网络时,端点给了我成功响应,但可达性仍然显示,未连接

提前致谢

【问题讨论】:

    标签: ios swift reachability


    【解决方案1】:

    听起来您的连接工作正常,但您很难获得是否已连接的指示。

    你可以

    1. 使用简单 Ping see here Ping 到本地“服务器”
    2. 对本地 Wifi 使用 Reachability 并检查 Wifi 名称(a.k.a. SSID)查看示例 here

    【讨论】:

      【解决方案2】:

      将此file 添加到您的项目中:

      然后配置您的 AppDelegate

      • application:didFinishLaunchingWithOptions:方法中

        let reach = Reachability()
        reach.monitorReachabilityChanges()
        
        NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.networkStatusChanged(_:)), name: NSNotification.Name(rawValue: ReachabilityStatusChangedNotification), object: nil)
        
      • 然后添加这个方法

         func networkStatusChanged(_ notification: Notification) {
            let userInfo = (notification as NSNotification).userInfo
            guard let status = userInfo?["Status"] as? String else { return }
            if status.lowercased().contains("online") {
                //CONNECTED
            } else {
                //NOT CONNECTED
            }
        }
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-29
        • 1970-01-01
        • 1970-01-01
        • 2013-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多