在WKWebview的Safari中打开新标签页

时间:2018-09-12 18:33:56

标签: ios swift wkwebview

我在Swift中有一个WKwebview,我正在尝试使要在网络浏览器中新标签页中打开的链接在浏览器safari移动版中打开,以便单击。我是这一领域的新手,不太了解,我的代码是这样的:

ViewController.swift

import UIKit
import WebKit

class ViewController: UIViewController {

    @IBOutlet weak var WebView: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "https://www.sitemercado.com.br/frade/")
        let request = URLRequest(url: url!)
        WebView.load(request)
    }

    // Verifica Conexao

    override func viewDidAppear(_ animated: Bool) {
        if CheckInternet.Connection() {

        } else {
            self.Alert(Message: "Você está off-line, tente novamente.")
        }
    }

    func Alert (Message: String) {
        let alert = UIAlertController(title: "Alert", message: Message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}

CheckInternet.swift

import Foundation
import SystemConfiguration

public class CheckInternet {

    class func Connection() -> 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(MemoryLayout.size(ofValue: zeroAddress))
        zeroAddress.sin_family = sa_family_t(AF_INET)

        let defaultRouteReachability = withUnsafePointer(to: &zeroAddress) {
            $0.withMemoryRebound(to: sockaddr.self, capacity: 1) {zeroSockAddress in
                SCNetworkReachabilityCreateWithAddress(nil, zeroSockAddress)
            }
        }

        var flags: SCNetworkReachabilityFlags = SCNetworkReachabilityFlags(rawValue: 0)
        if SCNetworkReachabilityGetFlags(defaultRouteReachability!, &flags) == false {
            return false
        }

        // Working for Cellular and WIFI

        let isReachable = (flags.rawValue & UInt32(kSCNetworkFlagsReachable)) != 0
        let needsConnection = (flags.rawValue & UInt32(kSCNetworkFlagsConnectionRequired)) != 0
        let ret = (isReachable && !needsConnection)

        return ret
    }
}

我也想当用户在没有Internet的情况下再次输入应用程序时再按一次“尝试”按钮,我已经进行了Internet检查,但是当再次单击“尝试”时,该如何在webview中重新加载

1 个答案:

答案 0 :(得分:0)

尝试通过这种方式更改您的上述代码(首先检查互联网连接,如果未连接,则发出警报)

import UIKit
import WebKit

class ViewController: UIViewController {

    @IBOutlet weak var WebView: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()

        if CheckInternet.Connection() {
            let url = URL(string: "https://www.sitemercado.com.br/frade/")
            let request = URLRequest(url: url!)
            WebView.load(request)
        } else {
            self.Alert(Message: "Você está off-line, tente novamente.")
        }
    }

    func Alert (Message: String) {
        let alert = UIAlertController(title: "Alert", message: Message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: "ok", style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)
    }
}