gpt4 book ai didi

ios - Apple Reachability 未显示警报

转载 作者:行者123 更新时间:2023-11-30 12:59:31 27 4
gpt4 key购买 nike

我在代码中使用了 Apple 的可达性类。下面是appdelegate的代码

        NSNotificationCenter.defaultCenter().addObserver(self, selector:"checkForReachability:", name: kReachabilityChangedNotification, object: nil);

self.reachability = Reachability.reachabilityForInternetConnection();
self.reachability.startNotifier();

这是 checkForReachability

func checkForReachability(notification:NSNotification){

let remoteHostStatus = self.reachability!.currentReachabilityStatus()
if (remoteHostStatus.rawValue == NotReachable.rawValue) {
print("NOT REACHABLE")
let myAlert = UIAlertController(title: "Alert", message: "Please check your internet connection.", preferredStyle: UIAlertControllerStyle.Alert)
let okAction = UIAlertAction(title:"Try again.", style:UIAlertActionStyle.Default) {
action in
self.checkForReachability(notification)
}
myAlert.addAction(okAction)
self.window?.rootViewController?.presentViewController(myAlert, animated:true, completion:nil)
}


}

但是当没有互联网时(当我关闭wifi时)它不会弹出警报

最佳答案

这可能对这种情况有帮助

我会给出最好的解决方案,希望它有帮助。

import Foundation
import SystemConfiguration

public class ReachabilityCheck {
class func isConnectionAvailable()->Bool{

var Status:Bool = false
let url = NSURL(string: "http://www.apple.com/")
let request = NSMutableURLRequest(URL: url!)
request.cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalAndRemoteCacheData
request.timeoutInterval = 20.0
request.HTTPMethod = "HEAD"

var response: NSURLResponse?

var data = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: nil) as NSData?

if let httpResponse = response as? NSHTTPURLResponse {
if httpResponse.statusCode == 200 {
Status = true
}

}
else
{

let alert = UIAlertView(title: "No Internet Connection", message: "Make sure you are connected to internet.", delegate: nil, cancelButtonTitle: "OK")
alert.show()
}

return Status
}
}

如何在其他类中访问或调用:

 if ReachabilityCheck.isConnectionAvailable() == true {

// write your code here when there is network

}

关于ios - Apple Reachability 未显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40010188/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com