I put Reachability.swift file in my app, when Internet reachability is changed, I want to alert to user that internet connection is not available.
this is my code.
import UIKit
import Parse
class ViewController: UIViewController {
var reachability : Reachability?
var myAlert = UIAlertController()
@IBOutlet weak var label: UILabel!
@IBOutlet weak var textField: UITextField!
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
do {
let reachability = try Reachability.reachabilityForInternetConnection()
self.reachability = reachability
} catch ReachabilityError.FailedToCreateWithAddress(let address) {
}
catch {}
NSNotificationCenter.defaultCenter().addObserver(self, selector: "HeyUserInternetDoesntWork", name: ReachabilityChangedNotification, object: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func saveButtonTapped(sender: AnyObject) {
let save = PFObject(className: "Practice")
save["text"] = textField.text
save.saveInBackgroundWithBlock { (success: Bool, error: NSError?) -> Void in
print("Object has been saved.")
}
}
dynamic func HeyUserInternetDoesntWork() {
if reachability!.isReachable() {
} else {
myAlert = UIAlertController(title: "No internet", message: "no good", preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction) }
}
}
this is not working, I got an error that
Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behaviour
I don't understand what this mean. If I put code that print("unreachable") will work fine.
My question
what is meaning of that error?
How I can make my alert works?
If there is other way to let user know Internet connectivity,
please let me know.
try to add this line under alert (self.presentViewController(myAlert, animated: true, completion: nil))