【问题标题】:How to perform a segue according to network connection?如何根据网络连接进行segue?
【发布时间】:2017-11-11 14:28:24
【问题描述】:

我是 Xcode 平台和 iOS 编程的新手。

  • 如果有网络连接,我想打开 webkit 视图。如果没有连接,我想打开另一个页面。
  • 我有一个第二页的 segue,ID 为“seque1”。

但我的代码不起作用。我不明白为什么。

import UIKit
import WebKit

class ViewController: UIViewController {
    @IBOutlet weak var webview: WKWebView!

    override func viewDidLoad() {
        super.viewDidLoad()

        if Reachability.isConnectedToNetwork(){
            print("Internet Connection Available!")
            let url = URL(string: "https://www.URL.com")
            let request = URLRequest(url: url!)
            webview.load(request)
        }else{
            print("Internet Connection not Available!")
            performSegue(withIdentifier: "seque1", sender: self)
        }
    }
}

【问题讨论】:

  • 当你展示你的 ViewController 时,你会根据网络状态得到正确的打印结果吗?
  • 在 viewDidAppear 中试试这个。
  • 请确保您的 segue 标识符正确,这里使用seque1 可能是segue1,请重新检查您的故事板

标签: ios swift webview webkit


【解决方案1】:

好问题...

你应该注意AppDelegate.swift 上的这些事件,所以你知道,在事情到手之前,你选择应该发生的事情

使用RootViewController,您决定首先要做什么,因此如果用户未连接,则将其转移到显示没有连接的视图控制器,如果连接,则继续操作。

将此代码粘贴到您的Appdelegate.swift

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {`
        let No_InternetViewController = NoInternetViewController() // your no internet vc
        let WebkitVC = WebkitVC() // orginial one


        if Reachability.isConnectedToNetwork(){

            self.window?.rootViewController = WebkitVC as! WebkitVC

        } else {

            print("Internet Connection not Available!")

            self.window?.rootViewController = No_InternetViewController as! No_InternetViewController
        }
       return true
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-21
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多