【问题标题】:I am getting error when trying to use Reachablitiy尝试使用可达性时出现错误
【发布时间】:2016-10-07 10:33:42
【问题描述】:

如果用户在我的 xcode swift 应用程序中没有 Internet 连接,我想提醒他们,因此我使用 Reachablity 来执行此操作。但是当我在 Viewcontroller 中实现我的代码时,我收到了这个错误:

预期声明

我也尝试将我的代码放入viewdidload,但没有成功。

这是代码:

 if Reachability.checkIntenetRechable() == false {
    let alertView = UIAlertCole: "APP_NAME", message: "Please check your internet connection.", preferredStyle: UIAlertControllerStyle.Alert)
    //alertView.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: { (action: UIAlertAction ) in }))
    alertView.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: { (action: UIAlertAction ) in
    // Put some code for okay button
    }))
    self.presentViewController(alertView, animated: true, completion: nil)
    }

【问题讨论】:

  • 我们在 Reachability.swift 文件中添加了 checkIntenetRechable() 方法。
  • @mr-bista 看起来我们的编辑发生了冲突或其他什么,你的我的无效,并且已经恢复了拼写错误和大写错误。 :/(固定)
  • 您是否在 Reachability 文件中添加了这个 checkIntenetRechable() ?如果没有,请根据我的帖子添加

标签: ios swift reachability


【解决方案1】:

您可能输入错误的方法。 我看到它有两个错别字。

你不是这个意思吗?

if Reachability.checkInternetReachable() == false { /* */ }

预期声明意味着您正在调用的方法不存在

【讨论】:

  • 如果您认为 OP 的问题是一个简单的拼写错误,您应该标记/投票以将问题关闭为“拼写错误或无法重现”。谢谢。
【解决方案2】:

Reachability 文件中似乎不存在该方法,所以在 Reachability 文件中添加此方法

class func checkIntenetRechable() -> Bool {

        var zeroAddress = sockaddr_in()
        zeroAddress.sin_len = UInt8(sizeofValue(zeroAddress))
        zeroAddress.sin_family = sa_family_t(AF_INET)
        let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
            SCNetworkReachabilityCreateWithAddress(nil, UnsafePointer($0))
        }
        var flags = SCNetworkReachabilityFlags()
        if !SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) {
            return false
        }
        let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
        let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0

        return (isReachable && !needsConnection) ? true : false
    }

【讨论】:

    猜你喜欢
    • 2014-05-24
    • 1970-01-01
    • 2020-06-24
    • 2014-02-05
    • 2016-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多