简体   繁体   中英

Open new tab in Safari in WKWebview

I have a WKwebview in Swift and I am trying to make the links that open in new tab in the web browser to be opened in the browser safari mobile to be clicked I am new in this area and do not understand very well, my code is this:

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
    }
}

Also I want to treat when the person enters the application without internet putting a try button again I already did the internet check but which way to give reload in webview when clicking try again

Try this way to change your above code (First check internet connection, if not connected give alert you want)

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)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM