2017-11-11 144 views
0

我是Xcode平臺和iOS編程的新手。如何根據網絡連接執行segue?

  • 我想打開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) 
     } 
    } 
} 
+0

當您出示您的ViewController你得到正確的打印依賴於網絡的狀態? –

+0

在viewDidAppear中試試這個。 –

+0

請確保您的segue標識符正確,在這裏您使用'seque1'可能是'segue1',請重新檢查您的故事板 – 3stud1ant3

回答

0

大問題...

你應該注意的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 
}