我正在创建一个应用程序,通过使用if else语句来检测是否存在与Internet的连接,当有互联网时,什么也不做,但如果没有互联网连接,则提醒视图说应用程序需要互联网我管理发现可达性但是在我的viewDidLoad()上实现uialertview似乎无法正常工作。我用这个:
public class Reachability {
class func isConnectedToNetwork() -> Bool {
var zeroAddress = sockaddr_in(sin_len: 0, sin_family: 0, sin_port: 0, sin_addr: in_addr(s_addr: 0), sin_zero: (0, 0, 0, 0, 0, 0, 0, 0))
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 = SCNetworkReachabilityFlags(rawValue: 0)
if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
return false
}
let isReachable = flags == .reachable
let needsConnection = flags == .connectionRequired
return isReachable && !needsConnection
}
}
然后在我的View Controller类中:
override func viewDidLoad() {
if Reachability.isConnectedToNetwork() == true
{
print("Connected")
}
else
{
let controller = UIAlertController(title: "No Internet Detected", message: "This app requires an Internet connection", preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default, handler: nil)
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
controller.addAction(ok)
controller.addAction(cancel)
present(controller, animated: true, completion: nil)
}
super.viewDidLoad()
//...
}
并且看起来警报视图没有弹出任何建议?或者帮忙?谢谢
您必须将AlertController放在ViewControllers生命周期的viewDidAppear方法中,因为在方法viewDidLoad和viewWillAppear中,您当前的视图仍然不在窗口层次结构中。 如果您想要提供更多ViewControllers,例如AlertController,您需要一个完整加载的视图,您可以在其中呈现新的ViewController:
override func viewDidLoad() {
super.viewDidLoad()
//ViewControllers view not in the window hierarchy
// Here you could make further initialization of your views subviews
}
override func viewWillAppear(_ animated: Bool) {
//ViewControllers view ist still not in the window hierarchy
//This is the right place to do for instance animations on your views subviews
}
override func viewDidAppear(_ animated: Bool) {
// ViewControllers view ist fully loaded and could present further ViewController
//Here you could do any other UI operations
if Reachability.isConnectedToNetwork() == true
{
print("Connected")
}
else
{
let controller = UIAlertController(title: "No Internet Detected", message: "This app requires an Internet connection", preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default, handler: nil)
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
controller.addAction(ok)
controller.addAction(cancel)
present(controller, animated: true, completion: nil)
}
}
这是Apple文档关于ViewController的说法:UIViewController方法被调用如下:
这是Apple文档关于ViewController的说法:UIViewController方法被调用如下:
// viewDidAppear
override func viewDidAppear(_ animated: Bool) {
netTester()
}
//Internet Checker
func netTester() {
reachability = Reachability.init()
if ((self.reachability!.connection) != .none) {
print("Internet is Ok")
} else{
print("Internet is nO ok")
let controller = UIAlertController(title: "No Internet Detected", message: "This app requires an Internet connection", preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default, handler: nil)
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
controller.addAction(ok)
controller.addAction(cancel)
present(controller, animated: true, completion: nil)
}
do {
try reachability!.startNotifier()
} catch {
print("Unable to start notifier")
}
}
View Did Load - 第一种方法,在第一次加载视图但在屏幕/窗口上没有出现时调用,仅加载。
只在第一次加载视图时调用一次。
查看已显示 - 在调用viewWillAppear后,将调用viewDidAppear。这意味着视图现在出现在屏幕上。
用户从该视图控制器移动到另一个视图控制器并返回时调用的次数。
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621423-viewdidappear