【问题标题】:Notify user reachability (Ashley Mills' Reachability)通知用户可达性(Ashley Mills 的可达性)
【发布时间】:2015-11-06 23:37:09
【问题描述】:

我制作了一个应用程序,我想添加在应用程序的互联网可达性发生变化时通知用户的功能。

我使用 Ashley Mills 的 Reachability.swift 文件。

现在我明白了它是如何工作的,所以我放了代码,当互联网可达性改变时,它会在 appDelegate 中打印它的状态。

但是,当我尝试输入提醒用户没有互联网连接的功能时,它会出错。

这是我在应用程序委托中的代码。

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

var reachability : Reachability?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
do {
        let reachability = try Reachability.reachabilityForInternetConnection()
        self.reachability = reachability
    } catch ReachabilityError.FailedToCreateWithAddress(let address) {

    }
    catch {}

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:", name: ReachabilityChangedNotification, object: reachability)
    do {
        try reachability?.startNotifier()
    } catch {}

    return true
}

func reachabilityChanged(notification: NSNotification) {
    let reachability = notification.object as! Reachability

    if reachability.isReachable() {

        print("reached")
    } else {


        print("not reached")
    }
}

这很好用。 然而Viewcontroller中的代码,

class ViewController: UIViewController {
var reachability : Reachability?
@IBOutlet weak var label: UILabel!
@IBOutlet weak var textField: UITextField!
override func viewDidLoad() {
    super.viewDidLoad()

NSNotificationCenter.defaultCenter().addObserver(self, selector: "HeyUserInternetDoesntWork", name: ReachabilityChangedNotification, object: nil)
    //get call from appDelegate Notification.
     }


func HeyUserInternetDoesntWork() {


    if reachability!.isReachable() {

        print("notify User working")
    } else {


        print("Notify user not working")
    }
}

在展开可选值时意外发现 nil

它得到这个错误。

我将在它工作后放置用于提醒用户的代码。

这里有问题,

我怎样才能使它工作? 它不一定要使用那个方法,但我想继续使用 NSNotification。 其实我是一个新人编码,所以请详细说明。

【问题讨论】:

    标签: ios swift reachability reachability-swift


    【解决方案1】:

    你在哪里初始化可达性属性?此变量始终为零。 在 func HeyUserInternetDoesntWork 中,您尝试使用可达性,当然它会出错。您需要像这样初始化属性:

    private let reachability = Reachability.reachabilityForInternetConnection()
    

    使用带有'dynamic'关键字的func HeyUserInternetDoesntWork后:

    dynamic func HeyUserInternetDoesntWork() {
    
    if reachability!.isReachable() {
    
        print("notify User working")
    } else {
    
    
        print("Notify user not working")
    }
    }
    

    因为 NSNotificationCenter 观察者选择器应该是动态的。

    【讨论】:

    • do { let reachability = try Reachability.reachabilityForInternetConnection() self.reachability = reachability } catch ReachabilityError.FailedToCreateWithAddress(let address) { } catch {} 我把这个代替了你的 init 属性,然后工作正常!谢谢
    • 哦,还有一个,我写了警报代码,但是我有这个错误。 > 2015-11-07 00:50:42.629 practiceParse[4799:2278456] 在解除分配时尝试加载视图控制器的视图是不允许的,并且可能导致未定义的行为你知道出了什么问题吗?
    • 请将此作为一个新问题提出
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多