我在我的代码中使用了Apple的可访问性类。以下是appdelegate的代码
self.resultLabel.text = (json as AnyObject).description
这是checkForReachability
NSNotificationCenter.defaultCenter().addObserver(self, selector:"checkForReachability:", name: kReachabilityChangedNotification, object: nil);
self.reachability = Reachability.reachabilityForInternetConnection();
self.reachability.startNotifier();
但是当没有互联网时(当我关闭wifi时)它不会弹出警报
答案 0 :(得分:0)
这可能有助于
我将提供最佳解决方案,希望它有所帮助。
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
}