我有一个应用程序,它是完全地图视图。我需要的是如果用户没有连接到互联网,我需要显示一些警报。我做到了。这是代码:
Reachability.swift.
import Foundation
import SystemConfiguration
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(sizeofValue(zeroAddress))
zeroAddress.sin_family = sa_family_t(AF_INET)
let defaultRouteReachability = withUnsafePointer(&zeroAddress) {
SCNetworkReachabilityCreateWithAddress(kCFAllocatorDefault, UnsafePointer($0))
}
var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
return false
}
let isReachable = flags == .Reachable
let needsConnection = flags == .ConnectionRequired
return isReachable && !needsConnection
}
}
if Reachability.isConnectedToNetwork() == true {
println("Internet connection OK")
} else {
println("Internet connection FAILED")
var alert = UIAlertView(title: "No Internet Connection", message: "Make sure your device is connected to the internet.", delegate: nil, cancelButtonTitle: "OK")
alert.show()
}
但我需要的是:当用户未连接到互联网时,会显示一条UIAlertView
消息。在那个警报中我有一个OK
按钮。因此,在用户连接到互联网之前,可以将它们重定向到移动数据开/关设置,或者可以显示UIAlertView
消息,直到用户连接到互联网..
请帮帮我!!
答案 0 :(得分:0)
点击alertview中的ok按钮,您可以将用户重定向到所需的页面,调用相应的segue。第二个选项,如果要阻止用户,只需显示包含自定义消息的活动指示符。
答案 1 :(得分:0)
有一件事是让用户了解网络状态。
构建应用程序以支持所有这些网络状态是完全不同的事情。
确保您的用户了解当前的网络状态,同时确保应用程序的行为与此相符。
不,您无法直接重定向到Wi-Fi设置。
答案 2 :(得分:0)
启动iOS设备设置页面
Step.1 转到项目设置 - >信息 - >网址类型 - >添加新的URL方案
Step.2 在viewWillAppear方法中检查互联网并显示提醒
override func viewWillAppear(animated: Bool)
{
//check for internet
if Reachability.isConnectedToNetwork() == true
{
print("Internet connection OK")
}
else
{
print("Internet connection off")
let alertView: UIAlertView = UIAlertView(title: "Alert!", message: "Please enable internet settings", delegate: self, cancelButtonTitle: "Settings", otherButtonTitles: "Cancel")
alertView.show()
return
}
}
Step.3 处理提醒按钮点击
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int)
{
if buttonIndex == 0
{
//This will open ios devices wifi settings
UIApplication.sharedApplication().openURL(NSURL(string: "prefs:root")!)
}
else if buttonIndex == 1
{
//TODO for cancel
}
}
注意:不要忘记确认UIAlertViewDelegate
点击此处完整示例代码我已经测试了
import UIKit
class ViewController: UIViewController, UIAlertViewDelegate
{
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(animated: Bool)
{
//check for internet
if Reachability.isConnectedToNetwork() == true
{
print("Internet connection OK")
}
else
{
print("Internet connection off")
let alertView: UIAlertView = UIAlertView(title: "Alert!", message: "Please enable internet settings", delegate: self, cancelButtonTitle: "Settings", otherButtonTitles: "Cancel")
alertView.show()
return
}
}
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int)
{
if buttonIndex == 0
{
//This will open ios devices wifi settings
UIApplication.sharedApplication().openURL(NSURL(string: "prefs:root")!)
}
else if buttonIndex == 1
{
//TODO for cancel
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 3 :(得分:0)
1-)您的应用目标>信息> URL类型然后添加像这样的新URL类型;
2-)在你的AppDelegate中,定义这个变量;
var isInternetConnected = false
3-)在UIViewController
中,定义一个AppDelegate变量,然后在UIViewController viewDidLoad()
方法中,开始监听连接;
let appDelegate: AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "internetNotifier:", name: kReachabilityChangedNotification, object: nil)
Reachability.reachabilityForInternetConnection().startNotifier()
}
4-)在你的internetNotifier方法中,检查连接;
func internetNotifier(notification: NSNotification) {
if let reachObject = notification.object as? Reachability {
switch reachObject.isReachable() {
case true:
appDelegate.isInternetConnected = true
hideAlertController()
case false:
appDelegate.isInternetConnected = false
presentViewController(UIAlertController.showAlertController(), animated: true, completion: nil)
}
}
}
func hideAlertController() {
if self.navigationController?.visibleViewController is UIAlertController {
dismissViewControllerAnimated(true, completion: nil)
}
}
5-)在UIAlertController
中创建AppDelegate
的新扩展名;
extension UIAlertController {
class final func showAlertController() -> UIAlertController {
let internetController = self.init(title: "Error", message: "No internet connection. Go to Settings and open your Wi-Fİ", preferredStyle: .Alert)
internetController.addAction(UIAlertAction(title: "Go to Settings", style: .Default, handler: { (internetController) -> Void in
if let settingsURL = NSURL(string: "prefs:root=WIFI") where UIApplication.sharedApplication().canOpenURL(settingsURL) {
dispatch_async(dispatch_get_main_queue()) {
UIApplication.sharedApplication().openURL(settingsURL)
}
}
}))
return internetController
}
}
6-)最后也是最重要的一步,在AppDelegate中调用你的UIAlertController
;
func applicationWillEnterForeground(application: UIApplication) {
if !isInternetConnected {
window?.rootViewController?.presentViewController(UIAlertController.showAlertController(), animated: true, completion: nil)
}
}
因为,如果用户进入设置打开网络然后返回您的应用程序而仍然没有连接,则需要再次向他显示您的警报。不要忘记在UIViewController
;
deinit {
NSNotificationCenter.defaultCenter().removeObserver(self, name: kReachabilityChangedNotification, object: nil)
Reachability.reachabilityForInternetConnection().stopNotifier()
}