0

我是 Swift 的初学者,我正在尝试提醒用户重新尝试连接到互联网。我正在使用 ashleymills 的可达性。我对在我的警报处理程序上放置什么感到困惑,因为这不起作用:

func alertConnect() -> UIViewController{
let reach = Reachability()
let alert = UIAlertController(title: "No Internet", message: "Tap to retry connect internet", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Retry", style: .default, handler: {
    action in
    do {
        try reach?.startNotifier()
    } catch {
        print("Unable to start notifier")
    }
}))
alert.addAction(UIAlertAction(title: Constants.cancel, style: .cancel))
return alert
}

这是我的可达性.class:

class ConnectionManager: NSObject {

var reachability: Reachability!

static let sharedInstance: ConnectionManager = { return ConnectionManager() }()


override init() {
    super.init()

    reachability = Reachability()!

    NotificationCenter.default.addObserver(
        self,
        selector: #selector(networkStatusChanged(_:)),
        name: .reachabilityChanged,
        object: reachability
    )

    do {
        try reachability.startNotifier()
    } catch {
        print("Unable to start notifier")
    }
}

@objc func networkStatusChanged(_ notification: Notification) {
    // Do something globally here!
    do {
        try reachability.startNotifier()
    } catch {
        print("Unable to start notifier")
    }
}

static func stopNotifier() -> Void {
    do {
        try (ConnectionManager.sharedInstance.reachability).startNotifier()
    } catch {
        print("Error stopping notifier")
    }
}

static func isReachable(completed: @escaping (ConnectionManager) -> Void) {
    if (ConnectionManager.sharedInstance.reachability).connection != .none {
        completed(ConnectionManager.sharedInstance)
    }
}

static func isUnreachable(completed: @escaping (ConnectionManager) -> Void) {
    if (ConnectionManager.sharedInstance.reachability).connection == .none {
        completed(ConnectionManager.sharedInstance)
    }
}

static func isReachableViaWWAN(completed: @escaping (ConnectionManager) -> Void) {
    if (ConnectionManager.sharedInstance.reachability).connection == .cellular {
        completed(ConnectionManager.sharedInstance)
    }
}

static func isReachableViaWiFi(completed: @escaping (ConnectionManager) -> Void) {
    if (ConnectionManager.sharedInstance.reachability).connection == .wifi {
        completed(ConnectionManager.sharedInstance)
    }
}    }

我应该在我的重试操作处理程序上放什么来让用户重新连接到互联网?谢谢你的帮助。

4

2 回答 2

0

我找到了解决问题的方法。由于 Reachability 检查应用程序是否可访问以及网络状态是否发生变化,所以我在 Retry 处理程序上所做的是在我的视图控制器上声明该函数,该函数在检查网络连接时再次调用 API。

于 2018-08-29T06:45:51.687 回答
0

Swift 4的工作代码

import SystemConfiguration

func InternetCheck () -> Bool {

    var zeroAddress = sockaddr_in()
    zeroAddress.sin_len = UInt8(MemoryLayout.size(ofValue: zeroAddress))
    zeroAddress.sin_family = sa_family_t(AF_INET)

    let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
            $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
            SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
        }
    }

    var flags = SCNetworkReachabilityFlags()
    if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
        return false
    }
    let isReachable = flags.contains(.reachable)
    let needsConnection = flags.contains(.connectionRequired)
    return (isReachable && !needsConnection)
}

检查

if InternetCheck() == false {

   let alert = UIAlertView(title:"Uhhhh :(",message: "No internet connection.",delegate: nil ,cancelButtonTitle: "Ok")
   alert.show()
   return

}
// Internet Connection Available
于 2018-08-28T05:44:17.727 回答